2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 * Created by Arjan van de Ven <arjanv@redhat.com>
7 * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
8 * University of Szeged, Hungary
10 * For licensing information, see the file 'LICENCE' in this directory.
12 * $Id: compr.c,v 1.46 2005/11/07 11:14:38 gleixner Exp $
18 static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
20 /* Available compressors are on this list */
21 static LIST_HEAD(jffs2_compressor_list);
23 /* Actual compression mode */
24 static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
26 /* Statistics for blocks stored without compression */
27 static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
30 * @data: Pointer to uncompressed data
31 * @cdata: Pointer to returned pointer to buffer for compressed data
32 * @datalen: On entry, holds the amount of data available for compression.
33 * On exit, expected to hold the amount of data actually compressed.
34 * @cdatalen: On entry, holds the amount of space available for compressed
35 * data. On exit, expected to hold the actual size of the compressed
38 * Returns: Lower byte to be stored with data indicating compression type used.
39 * Zero is used to show that the data could not be compressed - the
40 * compressed version was actually larger than the original.
41 * Upper byte will be used later. (soon)
43 * If the cdata buffer isn't large enough to hold all the uncompressed data,
44 * jffs2_compress should compress as much as will fit, and should set
45 * *datalen accordingly to show the amount of data which were compressed.
47 uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
48 unsigned char *data_in, unsigned char **cpage_out,
49 uint32_t *datalen, uint32_t *cdatalen)
51 int ret = JFFS2_COMPR_NONE;
53 struct jffs2_compressor *this, *best=NULL;
54 unsigned char *output_buf = NULL, *tmp_buf;
55 uint32_t orig_slen, orig_dlen;
56 uint32_t best_slen=0, best_dlen=0;
58 switch (jffs2_compression_mode) {
59 case JFFS2_COMPR_MODE_NONE:
61 case JFFS2_COMPR_MODE_PRIORITY:
62 output_buf = kmalloc(*cdatalen,GFP_KERNEL);
64 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
68 orig_dlen = *cdatalen;
69 spin_lock(&jffs2_compressor_list_lock);
70 list_for_each_entry(this, &jffs2_compressor_list, list) {
71 /* Skip decompress-only backwards-compatibility and disabled modules */
72 if ((!this->compress)||(this->disabled))
76 spin_unlock(&jffs2_compressor_list_lock);
78 *cdatalen = orig_dlen;
79 compr_ret = this->compress(data_in, output_buf, datalen, cdatalen, NULL);
80 spin_lock(&jffs2_compressor_list_lock);
84 this->stat_compr_blocks++;
85 this->stat_compr_orig_size += *datalen;
86 this->stat_compr_new_size += *cdatalen;
90 spin_unlock(&jffs2_compressor_list_lock);
91 if (ret == JFFS2_COMPR_NONE) kfree(output_buf);
93 case JFFS2_COMPR_MODE_SIZE:
95 orig_dlen = *cdatalen;
96 spin_lock(&jffs2_compressor_list_lock);
97 list_for_each_entry(this, &jffs2_compressor_list, list) {
98 /* Skip decompress-only backwards-compatibility and disabled modules */
99 if ((!this->compress)||(this->disabled))
101 /* Allocating memory for output buffer if necessary */
102 if ((this->compr_buf_size<orig_dlen)&&(this->compr_buf)) {
103 spin_unlock(&jffs2_compressor_list_lock);
104 kfree(this->compr_buf);
105 spin_lock(&jffs2_compressor_list_lock);
106 this->compr_buf_size=0;
107 this->compr_buf=NULL;
109 if (!this->compr_buf) {
110 spin_unlock(&jffs2_compressor_list_lock);
111 tmp_buf = kmalloc(orig_dlen,GFP_KERNEL);
112 spin_lock(&jffs2_compressor_list_lock);
114 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
118 this->compr_buf = tmp_buf;
119 this->compr_buf_size = orig_dlen;
123 spin_unlock(&jffs2_compressor_list_lock);
124 *datalen = orig_slen;
125 *cdatalen = orig_dlen;
126 compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen, NULL);
127 spin_lock(&jffs2_compressor_list_lock);
130 if ((!best_dlen)||(best_dlen>*cdatalen)) {
131 best_dlen = *cdatalen;
132 best_slen = *datalen;
138 *cdatalen = best_dlen;
139 *datalen = best_slen;
140 output_buf = best->compr_buf;
141 best->compr_buf = NULL;
142 best->compr_buf_size = 0;
143 best->stat_compr_blocks++;
144 best->stat_compr_orig_size += best_slen;
145 best->stat_compr_new_size += best_dlen;
148 spin_unlock(&jffs2_compressor_list_lock);
151 printk(KERN_ERR "JFFS2: unknow compression mode.\n");
154 if (ret == JFFS2_COMPR_NONE) {
155 *cpage_out = data_in;
156 *datalen = *cdatalen;
157 none_stat_compr_blocks++;
158 none_stat_compr_size += *datalen;
161 *cpage_out = output_buf;
166 int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
167 uint16_t comprtype, unsigned char *cdata_in,
168 unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
170 struct jffs2_compressor *this;
173 /* Older code had a bug where it would write non-zero 'usercompr'
174 fields. Deal with it. */
175 if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
178 switch (comprtype & 0xff) {
179 case JFFS2_COMPR_NONE:
180 /* This should be special-cased elsewhere, but we might as well deal with it */
181 memcpy(data_out, cdata_in, datalen);
182 none_stat_decompr_blocks++;
184 case JFFS2_COMPR_ZERO:
185 memset(data_out, 0, datalen);
188 spin_lock(&jffs2_compressor_list_lock);
189 list_for_each_entry(this, &jffs2_compressor_list, list) {
190 if (comprtype == this->compr) {
192 spin_unlock(&jffs2_compressor_list_lock);
193 ret = this->decompress(cdata_in, data_out, cdatalen, datalen, NULL);
194 spin_lock(&jffs2_compressor_list_lock);
196 printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
199 this->stat_decompr_blocks++;
202 spin_unlock(&jffs2_compressor_list_lock);
206 printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
207 spin_unlock(&jffs2_compressor_list_lock);
213 int jffs2_register_compressor(struct jffs2_compressor *comp)
215 struct jffs2_compressor *this;
218 printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
221 comp->compr_buf_size=0;
222 comp->compr_buf=NULL;
224 comp->stat_compr_orig_size=0;
225 comp->stat_compr_new_size=0;
226 comp->stat_compr_blocks=0;
227 comp->stat_decompr_blocks=0;
228 D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
230 spin_lock(&jffs2_compressor_list_lock);
232 list_for_each_entry(this, &jffs2_compressor_list, list) {
233 if (this->priority < comp->priority) {
234 list_add(&comp->list, this->list.prev);
238 list_add_tail(&comp->list, &jffs2_compressor_list);
240 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
241 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
244 spin_unlock(&jffs2_compressor_list_lock);
249 int jffs2_unregister_compressor(struct jffs2_compressor *comp)
251 D2(struct jffs2_compressor *this;)
253 D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
255 spin_lock(&jffs2_compressor_list_lock);
257 if (comp->usecount) {
258 spin_unlock(&jffs2_compressor_list_lock);
259 printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
262 list_del(&comp->list);
264 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
265 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
267 spin_unlock(&jffs2_compressor_list_lock);
271 #ifdef CONFIG_JFFS2_PROC
273 #define JFFS2_STAT_BUF_SIZE 16000
275 char *jffs2_list_compressors(void)
277 struct jffs2_compressor *this;
280 act_buf = buf = kmalloc(JFFS2_STAT_BUF_SIZE,GFP_KERNEL);
281 list_for_each_entry(this, &jffs2_compressor_list, list) {
282 act_buf += sprintf(act_buf, "%10s priority:%d ", this->name, this->priority);
283 if ((this->disabled)||(!this->compress))
284 act_buf += sprintf(act_buf,"disabled");
286 act_buf += sprintf(act_buf,"enabled");
287 act_buf += sprintf(act_buf,"\n");
292 char *jffs2_stats(void)
294 struct jffs2_compressor *this;
297 act_buf = buf = kmalloc(JFFS2_STAT_BUF_SIZE,GFP_KERNEL);
299 act_buf += sprintf(act_buf,"JFFS2 compressor statistics:\n");
300 act_buf += sprintf(act_buf,"%10s ","none");
301 act_buf += sprintf(act_buf,"compr: %d blocks (%d) decompr: %d blocks\n", none_stat_compr_blocks,
302 none_stat_compr_size, none_stat_decompr_blocks);
303 spin_lock(&jffs2_compressor_list_lock);
304 list_for_each_entry(this, &jffs2_compressor_list, list) {
305 act_buf += sprintf(act_buf,"%10s ",this->name);
306 if ((this->disabled)||(!this->compress))
307 act_buf += sprintf(act_buf,"- ");
309 act_buf += sprintf(act_buf,"+ ");
310 act_buf += sprintf(act_buf,"compr: %d blocks (%d/%d) decompr: %d blocks ", this->stat_compr_blocks,
311 this->stat_compr_new_size, this->stat_compr_orig_size,
312 this->stat_decompr_blocks);
313 act_buf += sprintf(act_buf,"\n");
315 spin_unlock(&jffs2_compressor_list_lock);
320 char *jffs2_get_compression_mode_name(void)
322 switch (jffs2_compression_mode) {
323 case JFFS2_COMPR_MODE_NONE:
325 case JFFS2_COMPR_MODE_PRIORITY:
327 case JFFS2_COMPR_MODE_SIZE:
333 int jffs2_set_compression_mode_name(const char *name)
335 if (!strcmp("none",name)) {
336 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
339 if (!strcmp("priority",name)) {
340 jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
343 if (!strcmp("size",name)) {
344 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
350 static int jffs2_compressor_Xable(const char *name, int disabled)
352 struct jffs2_compressor *this;
353 spin_lock(&jffs2_compressor_list_lock);
354 list_for_each_entry(this, &jffs2_compressor_list, list) {
355 if (!strcmp(this->name, name)) {
356 this->disabled = disabled;
357 spin_unlock(&jffs2_compressor_list_lock);
361 spin_unlock(&jffs2_compressor_list_lock);
362 printk(KERN_WARNING "JFFS2: compressor %s not found.\n",name);
366 int jffs2_enable_compressor_name(const char *name)
368 return jffs2_compressor_Xable(name, 0);
371 int jffs2_disable_compressor_name(const char *name)
373 return jffs2_compressor_Xable(name, 1);
376 int jffs2_set_compressor_priority(const char *name, int priority)
378 struct jffs2_compressor *this,*comp;
379 spin_lock(&jffs2_compressor_list_lock);
380 list_for_each_entry(this, &jffs2_compressor_list, list) {
381 if (!strcmp(this->name, name)) {
382 this->priority = priority;
387 spin_unlock(&jffs2_compressor_list_lock);
388 printk(KERN_WARNING "JFFS2: compressor %s not found.\n",name);
391 /* list is sorted in the order of priority, so if
392 we change it we have to reinsert it into the
394 list_del(&comp->list);
395 list_for_each_entry(this, &jffs2_compressor_list, list) {
396 if (this->priority < comp->priority) {
397 list_add(&comp->list, this->list.prev);
398 spin_unlock(&jffs2_compressor_list_lock);
402 list_add_tail(&comp->list, &jffs2_compressor_list);
403 spin_unlock(&jffs2_compressor_list_lock);
409 void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
411 if (orig != comprbuf)
415 int __init jffs2_compressors_init(void)
417 /* Registering compressors */
418 #ifdef CONFIG_JFFS2_ZLIB
421 #ifdef CONFIG_JFFS2_RTIME
424 #ifdef CONFIG_JFFS2_RUBIN
425 jffs2_rubinmips_init();
426 jffs2_dynrubin_init();
428 /* Setting default compression mode */
429 #ifdef CONFIG_JFFS2_CMODE_NONE
430 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
431 D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
433 #ifdef CONFIG_JFFS2_CMODE_SIZE
434 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
435 D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
437 D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
443 int __exit jffs2_compressors_exit(void)
445 /* Unregistering compressors */
446 #ifdef CONFIG_JFFS2_RUBIN
447 jffs2_dynrubin_exit();
448 jffs2_rubinmips_exit();
450 #ifdef CONFIG_JFFS2_RTIME
453 #ifdef CONFIG_JFFS2_ZLIB