MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / jffs2 / compr.c.org
blob46f182992c1605b724ab5132a4d43df6517d0803
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  * Created by Arjan van de Ven <arjanv@redhat.com>
6  *
7  * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
8  *                    University of Szeged, Hungary
9  *
10  * For licensing information, see the file 'LICENCE' in this directory.
11  *
12  * $Id: compr.c,v 1.46 2005/11/07 11:14:38 gleixner Exp $
13  *
14  */
16 #include "compr.h"
18 //static DEFINE_SPINLOCK(jffs2_compressor_list_lock);//johnson remove
19 static spinlock_t jffs2_compressor_list_lock = SPIN_LOCK_UNLOCKED;//johnson add
21 /* Available compressors are on this list */
22 static LIST_HEAD(jffs2_compressor_list);
24 /* Actual compression mode */
25 static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
27 /* Statistics for blocks stored without compression */
28 static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
30 /* jffs2_compress:
31  * @data: Pointer to uncompressed data
32  * @cdata: Pointer to returned pointer to buffer for compressed data
33  * @datalen: On entry, holds the amount of data available for compression.
34  *      On exit, expected to hold the amount of data actually compressed.
35  * @cdatalen: On entry, holds the amount of space available for compressed
36  *      data. On exit, expected to hold the actual size of the compressed
37  *      data.
38  *
39  * Returns: Lower byte to be stored with data indicating compression type used.
40  * Zero is used to show that the data could not be compressed - the
41  * compressed version was actually larger than the original.
42  * Upper byte will be used later. (soon)
43  *
44  * If the cdata buffer isn't large enough to hold all the uncompressed data,
45  * jffs2_compress should compress as much as will fit, and should set
46  * *datalen accordingly to show the amount of data which were compressed.
47  */
48 uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
49                              unsigned char *data_in, unsigned char **cpage_out,
50                              uint32_t *datalen, uint32_t *cdatalen)
52         int ret = JFFS2_COMPR_NONE;
53         int compr_ret;
54         struct jffs2_compressor *this, *best=NULL;
55         unsigned char *output_buf = NULL, *tmp_buf;
56         uint32_t orig_slen, orig_dlen;
57         uint32_t best_slen=0, best_dlen=0;
59         switch (jffs2_compression_mode) {
60         case JFFS2_COMPR_MODE_NONE:
61                 break;
62         case JFFS2_COMPR_MODE_PRIORITY:
63                 output_buf = kmalloc(*cdatalen,GFP_KERNEL);
64                 if (!output_buf) {
65                         printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
66                         goto out;
67                 }
68                 orig_slen = *datalen;
69                 orig_dlen = *cdatalen;
70                 spin_lock(&jffs2_compressor_list_lock);
71                 list_for_each_entry(this, &jffs2_compressor_list, list) {
72                         /* Skip decompress-only backwards-compatibility and disabled modules */
73                         if ((!this->compress)||(this->disabled))
74                                 continue;
76                         this->usecount++;
77                         spin_unlock(&jffs2_compressor_list_lock);
78                         *datalen  = orig_slen;
79                         *cdatalen = orig_dlen;
80                         compr_ret = this->compress(data_in, output_buf, datalen, cdatalen, NULL);
81                         spin_lock(&jffs2_compressor_list_lock);
82                         this->usecount--;
83                         if (!compr_ret) {
84                                 ret = this->compr;
85                                 this->stat_compr_blocks++;
86                                 this->stat_compr_orig_size += *datalen;
87                                 this->stat_compr_new_size  += *cdatalen;
88                                 break;
89                         }
90                 }
91                 spin_unlock(&jffs2_compressor_list_lock);
92                 if (ret == JFFS2_COMPR_NONE) kfree(output_buf);
93                 break;
94         case JFFS2_COMPR_MODE_SIZE:
95                 orig_slen = *datalen;
96                 orig_dlen = *cdatalen;
97                 spin_lock(&jffs2_compressor_list_lock);
98                 list_for_each_entry(this, &jffs2_compressor_list, list) {
99                         /* Skip decompress-only backwards-compatibility and disabled modules */
100                         if ((!this->compress)||(this->disabled))
101                                 continue;
102                         /* Allocating memory for output buffer if necessary */
103                         if ((this->compr_buf_size<orig_dlen)&&(this->compr_buf)) {
104                                 spin_unlock(&jffs2_compressor_list_lock);
105                                 kfree(this->compr_buf);
106                                 spin_lock(&jffs2_compressor_list_lock);
107                                 this->compr_buf_size=0;
108                                 this->compr_buf=NULL;
109                         }
110                         if (!this->compr_buf) {
111                                 spin_unlock(&jffs2_compressor_list_lock);
112                                 tmp_buf = kmalloc(orig_dlen,GFP_KERNEL);
113                                 spin_lock(&jffs2_compressor_list_lock);
114                                 if (!tmp_buf) {
115                                         printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
116                                         continue;
117                                 }
118                                 else {
119                                         this->compr_buf = tmp_buf;
120                                         this->compr_buf_size = orig_dlen;
121                                 }
122                         }
123                         this->usecount++;
124                         spin_unlock(&jffs2_compressor_list_lock);
125                         *datalen  = orig_slen;
126                         *cdatalen = orig_dlen;
127                         compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen, NULL);
128                         spin_lock(&jffs2_compressor_list_lock);
129                         this->usecount--;
130                         if (!compr_ret) {
131                                 if ((!best_dlen)||(best_dlen>*cdatalen)) {
132                                         best_dlen = *cdatalen;
133                                         best_slen = *datalen;
134                                         best = this;
135                                 }
136                         }
137                 }
138                 if (best_dlen) {
139                         *cdatalen = best_dlen;
140                         *datalen  = best_slen;
141                         output_buf = best->compr_buf;
142                         best->compr_buf = NULL;
143                         best->compr_buf_size = 0;
144                         best->stat_compr_blocks++;
145                         best->stat_compr_orig_size += best_slen;
146                         best->stat_compr_new_size  += best_dlen;
147                         ret = best->compr;
148                 }
149                 spin_unlock(&jffs2_compressor_list_lock);
150                 break;
151         default:
152                 printk(KERN_ERR "JFFS2: unknow compression mode.\n");
153         }
154  out:
155         if (ret == JFFS2_COMPR_NONE) {
156                 *cpage_out = data_in;
157                 *datalen = *cdatalen;
158                 none_stat_compr_blocks++;
159                 none_stat_compr_size += *datalen;
160         }
161         else {
162                 *cpage_out = output_buf;
163         }
164         return ret;
167 int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
168                      uint16_t comprtype, unsigned char *cdata_in,
169                      unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
171         struct jffs2_compressor *this;
172         int ret;
174         /* Older code had a bug where it would write non-zero 'usercompr'
175            fields. Deal with it. */
176         if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
177                 comprtype &= 0xff;
179         switch (comprtype & 0xff) {
180         case JFFS2_COMPR_NONE:
181                 /* This should be special-cased elsewhere, but we might as well deal with it */
182                 memcpy(data_out, cdata_in, datalen);
183                 none_stat_decompr_blocks++;
184                 break;
185         case JFFS2_COMPR_ZERO:
186                 memset(data_out, 0, datalen);
187                 break;
188         default:
189                 spin_lock(&jffs2_compressor_list_lock);
190                 list_for_each_entry(this, &jffs2_compressor_list, list) {
191                         if (comprtype == this->compr) {
192                                 this->usecount++;
193                                 spin_unlock(&jffs2_compressor_list_lock);
194                                 ret = this->decompress(cdata_in, data_out, cdatalen, datalen, NULL);
195                                 spin_lock(&jffs2_compressor_list_lock);
196                                 if (ret) {
197                                         printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
198                                 }
199                                 else {
200                                         this->stat_decompr_blocks++;
201                                 }
202                                 this->usecount--;
203                                 spin_unlock(&jffs2_compressor_list_lock);
204                                 return ret;
205                         }
206                 }
207                 printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
208                 spin_unlock(&jffs2_compressor_list_lock);
209                 return -EIO;
210         }
211         return 0;
214 int jffs2_register_compressor(struct jffs2_compressor *comp)
216         struct jffs2_compressor *this;
218         if (!comp->name) {
219                 printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
220                 return -1;
221         }
222         comp->compr_buf_size=0;
223         comp->compr_buf=NULL;
224         comp->usecount=0;
225         comp->stat_compr_orig_size=0;
226         comp->stat_compr_new_size=0;
227         comp->stat_compr_blocks=0;
228         comp->stat_decompr_blocks=0;
229         D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
231         spin_lock(&jffs2_compressor_list_lock);
233         list_for_each_entry(this, &jffs2_compressor_list, list) {
234                 if (this->priority < comp->priority) {
235                         list_add(&comp->list, this->list.prev);
236                         goto out;
237                 }
238         }
239         list_add_tail(&comp->list, &jffs2_compressor_list);
240 out:
241         D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
242                 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
243         })
245         spin_unlock(&jffs2_compressor_list_lock);
247         return 0;
250 int jffs2_unregister_compressor(struct jffs2_compressor *comp)
252         D2(struct jffs2_compressor *this;)
254         D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
256         spin_lock(&jffs2_compressor_list_lock);
258         if (comp->usecount) {
259                 spin_unlock(&jffs2_compressor_list_lock);
260                 printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
261                 return -1;
262         }
263         list_del(&comp->list);
265         D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
266                 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
267         })
268         spin_unlock(&jffs2_compressor_list_lock);
269         return 0;
272 #ifdef CONFIG_JFFS2_PROC
274 #define JFFS2_STAT_BUF_SIZE 16000
276 char *jffs2_list_compressors(void)
278         struct jffs2_compressor *this;
279         char *buf, *act_buf;
281         act_buf = buf = kmalloc(JFFS2_STAT_BUF_SIZE,GFP_KERNEL);
282         list_for_each_entry(this, &jffs2_compressor_list, list) {
283                 act_buf += sprintf(act_buf, "%10s priority:%d ", this->name, this->priority);
284                 if ((this->disabled)||(!this->compress))
285                         act_buf += sprintf(act_buf,"disabled");
286                 else
287                         act_buf += sprintf(act_buf,"enabled");
288                 act_buf += sprintf(act_buf,"\n");
289         }
290         return buf;
293 char *jffs2_stats(void)
295         struct jffs2_compressor *this;
296         char *buf, *act_buf;
298         act_buf = buf = kmalloc(JFFS2_STAT_BUF_SIZE,GFP_KERNEL);
300         act_buf += sprintf(act_buf,"JFFS2 compressor statistics:\n");
301         act_buf += sprintf(act_buf,"%10s   ","none");
302         act_buf += sprintf(act_buf,"compr: %d blocks (%d)  decompr: %d blocks\n", none_stat_compr_blocks,
303                            none_stat_compr_size, none_stat_decompr_blocks);
304         spin_lock(&jffs2_compressor_list_lock);
305         list_for_each_entry(this, &jffs2_compressor_list, list) {
306                 act_buf += sprintf(act_buf,"%10s ",this->name);
307                 if ((this->disabled)||(!this->compress))
308                         act_buf += sprintf(act_buf,"- ");
309                 else
310                         act_buf += sprintf(act_buf,"+ ");
311                 act_buf += sprintf(act_buf,"compr: %d blocks (%d/%d)  decompr: %d blocks ", this->stat_compr_blocks,
312                                    this->stat_compr_new_size, this->stat_compr_orig_size,
313                                    this->stat_decompr_blocks);
314                 act_buf += sprintf(act_buf,"\n");
315         }
316         spin_unlock(&jffs2_compressor_list_lock);
318         return buf;
321 char *jffs2_get_compression_mode_name(void)
323         switch (jffs2_compression_mode) {
324         case JFFS2_COMPR_MODE_NONE:
325                 return "none";
326         case JFFS2_COMPR_MODE_PRIORITY:
327                 return "priority";
328         case JFFS2_COMPR_MODE_SIZE:
329                 return "size";
330         }
331         return "unkown";
334 int jffs2_set_compression_mode_name(const char *name)
336         if (!strcmp("none",name)) {
337                 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
338                 return 0;
339         }
340         if (!strcmp("priority",name)) {
341                 jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
342                 return 0;
343         }
344         if (!strcmp("size",name)) {
345                 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
346                 return 0;
347         }
348         return 1;
351 static int jffs2_compressor_Xable(const char *name, int disabled)
353         struct jffs2_compressor *this;
354         spin_lock(&jffs2_compressor_list_lock);
355         list_for_each_entry(this, &jffs2_compressor_list, list) {
356                 if (!strcmp(this->name, name)) {
357                         this->disabled = disabled;
358                         spin_unlock(&jffs2_compressor_list_lock);
359                         return 0;
360                 }
361         }
362         spin_unlock(&jffs2_compressor_list_lock);
363         printk(KERN_WARNING "JFFS2: compressor %s not found.\n",name);
364         return 1;
367 int jffs2_enable_compressor_name(const char *name)
369         return jffs2_compressor_Xable(name, 0);
372 int jffs2_disable_compressor_name(const char *name)
374         return jffs2_compressor_Xable(name, 1);
377 int jffs2_set_compressor_priority(const char *name, int priority)
379         struct jffs2_compressor *this,*comp;
380         spin_lock(&jffs2_compressor_list_lock);
381         list_for_each_entry(this, &jffs2_compressor_list, list) {
382                 if (!strcmp(this->name, name)) {
383                         this->priority = priority;
384                         comp = this;
385                         goto reinsert;
386                 }
387         }
388         spin_unlock(&jffs2_compressor_list_lock);
389         printk(KERN_WARNING "JFFS2: compressor %s not found.\n",name);
390         return 1;
391 reinsert:
392         /* list is sorted in the order of priority, so if
393            we change it we have to reinsert it into the
394            good place */
395         list_del(&comp->list);
396         list_for_each_entry(this, &jffs2_compressor_list, list) {
397                 if (this->priority < comp->priority) {
398                         list_add(&comp->list, this->list.prev);
399                         spin_unlock(&jffs2_compressor_list_lock);
400                         return 0;
401                 }
402         }
403         list_add_tail(&comp->list, &jffs2_compressor_list);
404         spin_unlock(&jffs2_compressor_list_lock);
405         return 0;
408 #endif
410 void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
412         if (orig != comprbuf)
413                 kfree(comprbuf);
416 int jffs2_compressors_init(void)
418 /* Registering compressors */
419 #ifdef CONFIG_JFFS2_ZLIB
420         jffs2_zlib_init();
421 #endif
422 #ifdef CONFIG_JFFS2_RTIME
423         jffs2_rtime_init();
424 #endif
425 #ifdef CONFIG_JFFS2_RUBIN
426         jffs2_rubinmips_init();
427         jffs2_dynrubin_init();
428 #endif
429 /* Setting default compression mode */
430 #ifdef CONFIG_JFFS2_CMODE_NONE
431         jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
432         D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
433 #else
434 #ifdef CONFIG_JFFS2_CMODE_SIZE
435         jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
436         D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
437 #else
438         D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
439 #endif
440 #endif
441         return 0;
444 int jffs2_compressors_exit(void)
446 /* Unregistering compressors */
447 #ifdef CONFIG_JFFS2_RUBIN
448         jffs2_dynrubin_exit();
449         jffs2_rubinmips_exit();
450 #endif
451 #ifdef CONFIG_JFFS2_RTIME
452         jffs2_rtime_exit();
453 #endif
454 #ifdef CONFIG_JFFS2_ZLIB
455         jffs2_zlib_exit();
456 #endif
457         return 0;