OSDN Git Service

Add MS7619SE
[uclinux-h8/uClinux-dist.git] / user / mtd-utils / compr.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
5  *                    University of Szeged, Hungary
6  *
7  * For licensing information, see the file 'LICENCE' in this directory
8  * in the jffs2 directory.
9  */
10
11 #include "compr.h"
12 #include <string.h>
13 #include <stdlib.h>
14 #include <linux/jffs2.h>
15
16 #define FAVOUR_LZO_PERCENT 80
17
18 extern int page_size;
19
20 /* LIST IMPLEMENTATION (from linux/list.h) */
21
22 #define LIST_HEAD_INIT(name) { &(name), &(name) }
23
24 #define LIST_HEAD(name) \
25         struct list_head name = LIST_HEAD_INIT(name)
26
27 static inline void __list_add(struct list_head *new,
28                 struct list_head *prev,
29                 struct list_head *next)
30 {
31         next->prev = new;
32         new->next = next;
33         new->prev = prev;
34         prev->next = new;
35 }
36
37 static inline void list_add(struct list_head *new, struct list_head *head)
38 {
39         __list_add(new, head, head->next);
40 }
41
42 static inline void list_add_tail(struct list_head *new, struct list_head *head)
43 {
44         __list_add(new, head->prev, head);
45 }
46
47 static inline void __list_del(struct list_head *prev, struct list_head *next)
48 {
49         next->prev = prev;
50         prev->next = next;
51 }
52
53 static inline void list_del(struct list_head *entry)
54 {
55         __list_del(entry->prev, entry->next);
56         entry->next = (void *) 0;
57         entry->prev = (void *) 0;
58 }
59
60 #define list_entry(ptr, type, member) \
61         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
62
63 #define list_for_each_entry(pos, head, member)                          \
64         for (pos = list_entry((head)->next, typeof(*pos), member);      \
65                         &pos->member != (head);                                    \
66                         pos = list_entry(pos->member.next, typeof(*pos), member))
67
68
69 /* Available compressors are on this list */
70 static LIST_HEAD(jffs2_compressor_list);
71
72 /* Actual compression mode */
73 static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
74
75 void jffs2_set_compression_mode(int mode)
76 {
77         jffs2_compression_mode = mode;
78 }
79
80 int jffs2_get_compression_mode(void)
81 {
82         return jffs2_compression_mode;
83 }
84
85 /* Statistics for blocks stored without compression */
86 static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
87
88 /* Compression test stuffs */
89
90 static int jffs2_compression_check = 0;
91
92 static unsigned char *jffs2_compression_check_buf = NULL;
93
94 void jffs2_compression_check_set(int yesno)
95 {
96         jffs2_compression_check = yesno;
97 }
98
99 int jffs2_compression_check_get(void)
100 {
101         return jffs2_compression_check;
102 }
103
104 static int jffs2_error_cnt = 0;
105
106 int jffs2_compression_check_errorcnt_get(void)
107 {
108         return jffs2_error_cnt;
109 }
110
111 #define JFFS2_BUFFER_FILL 0x55
112
113 /* Called before compression (if compression_check is setted) to prepare
114    the buffer for buffer overflow test */
115 static void jffs2_decompression_test_prepare(unsigned char *buf, int size)
116 {
117         memset(buf,JFFS2_BUFFER_FILL,size+1);
118 }
119
120 /* Called after compression (if compression_check is setted) to test the result */
121 static void jffs2_decompression_test(struct jffs2_compressor *compr,
122                 unsigned char *data_in, unsigned char *output_buf,
123                 uint32_t cdatalen, uint32_t datalen, uint32_t buf_size)
124 {
125         uint32_t i;
126
127         /* buffer overflow test */
128         for (i=buf_size;i>cdatalen;i--) {
129                 if (output_buf[i]!=JFFS2_BUFFER_FILL) {
130                         fprintf(stderr,"COMPR_ERROR: buffer overflow at %s. "
131                                         "(bs=%d csize=%d b[%d]=%d)\n", compr->name,
132                                         buf_size, cdatalen, i, (int)(output_buf[i]));
133                         jffs2_error_cnt++;
134                         return;
135                 }
136         }
137         /* allocing temporary buffer for decompression */
138         if (!jffs2_compression_check_buf) {
139                 jffs2_compression_check_buf = malloc(page_size);
140                 if (!jffs2_compression_check_buf) {
141                         fprintf(stderr,"No memory for buffer allocation. Compression check disabled.\n");
142                         jffs2_compression_check = 0;
143                         return;
144                 }
145         }
146         /* decompressing */
147         if (!compr->decompress) {
148                 fprintf(stderr,"JFFS2 compression check: there is no decompress function at %s.\n", compr->name);
149                 jffs2_error_cnt++;
150                 return;
151         }
152         if (compr->decompress(output_buf,jffs2_compression_check_buf,cdatalen,datalen)) {
153                 fprintf(stderr,"JFFS2 compression check: decompression failed at %s.\n", compr->name);
154                 jffs2_error_cnt++;
155         }
156         /* validate decompression */
157         else {
158                 for (i=0;i<datalen;i++) {
159                         if (data_in[i]!=jffs2_compression_check_buf[i]) {
160                                 fprintf(stderr,"JFFS2 compression check: data mismatch at %s (pos %d).\n", compr->name, i);
161                                 jffs2_error_cnt++;
162                                 break;
163                         }
164                 }
165         }
166 }
167
168 /*
169  * Return 1 to use this compression
170  */
171 static int jffs2_is_best_compression(struct jffs2_compressor *this,
172                 struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
173 {
174         switch (jffs2_compression_mode) {
175         case JFFS2_COMPR_MODE_SIZE:
176                 if (bestsize > size)
177                         return 1;
178                 return 0;
179         case JFFS2_COMPR_MODE_FAVOURLZO:
180                 if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
181                         return 1;
182                 if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
183                         return 1;
184                 if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
185                         return 1;
186                 if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
187                         return 1;
188
189                 return 0;
190         }
191         /* Shouldn't happen */
192         return 0;
193 }
194
195 /* jffs2_compress:
196  * @data: Pointer to uncompressed data
197  * @cdata: Pointer to returned pointer to buffer for compressed data
198  * @datalen: On entry, holds the amount of data available for compression.
199  *      On exit, expected to hold the amount of data actually compressed.
200  * @cdatalen: On entry, holds the amount of space available for compressed
201  *      data. On exit, expected to hold the actual size of the compressed
202  *      data.
203  *
204  * Returns: Lower byte to be stored with data indicating compression type used.
205  * Zero is used to show that the data could not be compressed - the
206  * compressed version was actually larger than the original.
207  * Upper byte will be used later. (soon)
208  *
209  * If the cdata buffer isn't large enough to hold all the uncompressed data,
210  * jffs2_compress should compress as much as will fit, and should set
211  * *datalen accordingly to show the amount of data which were compressed.
212  */
213 uint16_t jffs2_compress( unsigned char *data_in, unsigned char **cpage_out,
214                 uint32_t *datalen, uint32_t *cdatalen)
215 {
216         int ret = JFFS2_COMPR_NONE;
217         int compr_ret;
218         struct jffs2_compressor *this, *best=NULL;
219         unsigned char *output_buf = NULL, *tmp_buf;
220         uint32_t orig_slen, orig_dlen;
221         uint32_t best_slen=0, best_dlen=0;
222
223         switch (jffs2_compression_mode) {
224                 case JFFS2_COMPR_MODE_NONE:
225                         break;
226                 case JFFS2_COMPR_MODE_PRIORITY:
227                         orig_slen = *datalen;
228                         orig_dlen = *cdatalen;
229                         output_buf = malloc(orig_dlen+jffs2_compression_check);
230                         if (!output_buf) {
231                                 fprintf(stderr,"mkfs.jffs2: No memory for compressor allocation. Compression failed.\n");
232                                 goto out;
233                         }
234                         list_for_each_entry(this, &jffs2_compressor_list, list) {
235                                 /* Skip decompress-only backwards-compatibility and disabled modules */
236                                 if ((!this->compress)||(this->disabled))
237                                         continue;
238
239                                 this->usecount++;
240
241                                 if (jffs2_compression_check) /*preparing output buffer for testing buffer overflow */
242                                         jffs2_decompression_test_prepare(output_buf, orig_dlen);
243
244                                 *datalen  = orig_slen;
245                                 *cdatalen = orig_dlen;
246                                 compr_ret = this->compress(data_in, output_buf, datalen, cdatalen);
247                                 this->usecount--;
248                                 if (!compr_ret) {
249                                         ret = this->compr;
250                                         this->stat_compr_blocks++;
251                                         this->stat_compr_orig_size += *datalen;
252                                         this->stat_compr_new_size  += *cdatalen;
253                                         if (jffs2_compression_check)
254                                                 jffs2_decompression_test(this, data_in, output_buf, *cdatalen, *datalen, orig_dlen);
255                                         break;
256                                 }
257                         }
258                         if (ret == JFFS2_COMPR_NONE) free(output_buf);
259                         break;
260                 case JFFS2_COMPR_MODE_FAVOURLZO:
261                 case JFFS2_COMPR_MODE_SIZE:
262                         orig_slen = *datalen;
263                         orig_dlen = *cdatalen;
264                         list_for_each_entry(this, &jffs2_compressor_list, list) {
265                                 uint32_t needed_buf_size;
266
267                                 if (jffs2_compression_mode == JFFS2_COMPR_MODE_FAVOURLZO)
268                                         needed_buf_size = orig_slen + jffs2_compression_check;
269                                 else
270                                         needed_buf_size = orig_dlen + jffs2_compression_check;
271
272                                 /* Skip decompress-only backwards-compatibility and disabled modules */
273                                 if ((!this->compress)||(this->disabled))
274                                         continue;
275                                 /* Allocating memory for output buffer if necessary */
276                                 if ((this->compr_buf_size < needed_buf_size) && (this->compr_buf)) {
277                                         free(this->compr_buf);
278                                         this->compr_buf_size=0;
279                                         this->compr_buf=NULL;
280                                 }
281                                 if (!this->compr_buf) {
282                                         tmp_buf = malloc(needed_buf_size);
283                                         if (!tmp_buf) {
284                                                 fprintf(stderr,"mkfs.jffs2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
285                                                 continue;
286                                         }
287                                         else {
288                                                 this->compr_buf = tmp_buf;
289                                                 this->compr_buf_size = orig_dlen;
290                                         }
291                                 }
292                                 this->usecount++;
293                                 if (jffs2_compression_check) /*preparing output buffer for testing buffer overflow */
294                                         jffs2_decompression_test_prepare(this->compr_buf,this->compr_buf_size);
295                                 *datalen  = orig_slen;
296                                 *cdatalen = orig_dlen;
297                                 compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
298                                 this->usecount--;
299                                 if (!compr_ret) {
300                                         if (jffs2_compression_check)
301                                                 jffs2_decompression_test(this, data_in, this->compr_buf, *cdatalen, *datalen, this->compr_buf_size);
302                                         if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
303                                                                 && (*cdatalen < *datalen)) {
304                                                 best_dlen = *cdatalen;
305                                                 best_slen = *datalen;
306                                                 best = this;
307                                         }
308                                 }
309                         }
310                         if (best_dlen) {
311                                 *cdatalen = best_dlen;
312                                 *datalen  = best_slen;
313                                 output_buf = best->compr_buf;
314                                 best->compr_buf = NULL;
315                                 best->compr_buf_size = 0;
316                                 best->stat_compr_blocks++;
317                                 best->stat_compr_orig_size += best_slen;
318                                 best->stat_compr_new_size  += best_dlen;
319                                 ret = best->compr;
320                         }
321                         break;
322                 default:
323                         fprintf(stderr,"mkfs.jffs2: unknow compression mode.\n");
324         }
325 out:
326         if (ret == JFFS2_COMPR_NONE) {
327                 *cpage_out = data_in;
328                 *datalen = *cdatalen;
329                 none_stat_compr_blocks++;
330                 none_stat_compr_size += *datalen;
331         }
332         else {
333                 *cpage_out = output_buf;
334         }
335         return ret;
336 }
337
338
339 int jffs2_register_compressor(struct jffs2_compressor *comp)
340 {
341         struct jffs2_compressor *this;
342
343         if (!comp->name) {
344                 fprintf(stderr,"NULL compressor name at registering JFFS2 compressor. Failed.\n");
345                 return -1;
346         }
347         comp->compr_buf_size=0;
348         comp->compr_buf=NULL;
349         comp->usecount=0;
350         comp->stat_compr_orig_size=0;
351         comp->stat_compr_new_size=0;
352         comp->stat_compr_blocks=0;
353         comp->stat_decompr_blocks=0;
354
355         list_for_each_entry(this, &jffs2_compressor_list, list) {
356                 if (this->priority < comp->priority) {
357                         list_add(&comp->list, this->list.prev);
358                         goto out;
359                 }
360         }
361         list_add_tail(&comp->list, &jffs2_compressor_list);
362 out:
363         return 0;
364 }
365
366 int jffs2_unregister_compressor(struct jffs2_compressor *comp)
367 {
368
369         if (comp->usecount) {
370                 fprintf(stderr,"mkfs.jffs2: Compressor modul is in use. Unregister failed.\n");
371                 return -1;
372         }
373         list_del(&comp->list);
374
375         return 0;
376 }
377
378 #define JFFS2_STAT_BUF_SIZE 16000
379
380 char *jffs2_list_compressors(void)
381 {
382         struct jffs2_compressor *this;
383         char *buf, *act_buf;
384
385         act_buf = buf = malloc(JFFS2_STAT_BUF_SIZE);
386         list_for_each_entry(this, &jffs2_compressor_list, list) {
387                 act_buf += sprintf(act_buf, "%10s priority:%d ", this->name, this->priority);
388                 if ((this->disabled)||(!this->compress))
389                         act_buf += sprintf(act_buf,"disabled");
390                 else
391                         act_buf += sprintf(act_buf,"enabled");
392                 act_buf += sprintf(act_buf,"\n");
393         }
394         return buf;
395 }
396
397 char *jffs2_stats(void)
398 {
399         struct jffs2_compressor *this;
400         char *buf, *act_buf;
401
402         act_buf = buf = malloc(JFFS2_STAT_BUF_SIZE);
403
404         act_buf += sprintf(act_buf,"Compression mode: ");
405         switch (jffs2_compression_mode) {
406                 case JFFS2_COMPR_MODE_NONE:
407                         act_buf += sprintf(act_buf,"none");
408                         break;
409                 case JFFS2_COMPR_MODE_PRIORITY:
410                         act_buf += sprintf(act_buf,"priority");
411                         break;
412                 case JFFS2_COMPR_MODE_SIZE:
413                         act_buf += sprintf(act_buf,"size");
414                         break;
415                 case JFFS2_COMPR_MODE_FAVOURLZO:
416                         act_buf += sprintf(act_buf, "favourlzo");
417                         break;
418                 default:
419                         act_buf += sprintf(act_buf,"unkown");
420                         break;
421         }
422         act_buf += sprintf(act_buf,"\nCompressors:\n");
423         act_buf += sprintf(act_buf,"%10s             ","none");
424         act_buf += sprintf(act_buf,"compr: %d blocks (%d)  decompr: %d blocks\n", none_stat_compr_blocks,
425                         none_stat_compr_size, none_stat_decompr_blocks);
426         list_for_each_entry(this, &jffs2_compressor_list, list) {
427                 act_buf += sprintf(act_buf,"%10s (prio:%d) ",this->name,this->priority);
428                 if ((this->disabled)||(!this->compress))
429                         act_buf += sprintf(act_buf,"- ");
430                 else
431                         act_buf += sprintf(act_buf,"+ ");
432                 act_buf += sprintf(act_buf,"compr: %d blocks (%d/%d)  decompr: %d blocks ", this->stat_compr_blocks,
433                                 this->stat_compr_new_size, this->stat_compr_orig_size,
434                                 this->stat_decompr_blocks);
435                 act_buf += sprintf(act_buf,"\n");
436         }
437         return buf;
438 }
439
440 int jffs2_set_compression_mode_name(const char *name)
441 {
442         if (!strcmp("none",name)) {
443                 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
444                 return 0;
445         }
446         if (!strcmp("priority",name)) {
447                 jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
448                 return 0;
449         }
450         if (!strcmp("size",name)) {
451                 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
452                 return 0;
453         }
454         if (!strcmp("favourlzo", name)) {
455                 jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
456                 return 0;
457         }
458
459         return 1;
460 }
461
462 static int jffs2_compressor_Xable(const char *name, int disabled)
463 {
464         struct jffs2_compressor *this;
465         list_for_each_entry(this, &jffs2_compressor_list, list) {
466                 if (!strcmp(this->name, name)) {
467                         this->disabled = disabled;
468                         return 0;
469                 }
470         }
471         return 1;
472 }
473
474 int jffs2_enable_compressor_name(const char *name)
475 {
476         return jffs2_compressor_Xable(name, 0);
477 }
478
479 int jffs2_disable_compressor_name(const char *name)
480 {
481         return jffs2_compressor_Xable(name, 1);
482 }
483
484 int jffs2_set_compressor_priority(const char *name, int priority)
485 {
486         struct jffs2_compressor *this,*comp;
487         list_for_each_entry(this, &jffs2_compressor_list, list) {
488                 if (!strcmp(this->name, name)) {
489                         this->priority = priority;
490                         comp = this;
491                         goto reinsert;
492                 }
493         }
494         fprintf(stderr,"mkfs.jffs2: compressor %s not found.\n",name);
495         return 1;
496 reinsert:
497         /* list is sorted in the order of priority, so if
498            we change it we have to reinsert it into the
499            good place */
500         list_del(&comp->list);
501         list_for_each_entry(this, &jffs2_compressor_list, list) {
502                 if (this->priority < comp->priority) {
503                         list_add(&comp->list, this->list.prev);
504                         return 0;
505                 }
506         }
507         list_add_tail(&comp->list, &jffs2_compressor_list);
508         return 0;
509 }
510
511
512 int jffs2_compressors_init(void)
513 {
514 #ifdef CONFIG_JFFS2_ZLIB
515         jffs2_zlib_init();
516 #endif
517 #ifdef CONFIG_JFFS2_RTIME
518         jffs2_rtime_init();
519 #endif
520 #ifdef CONFIG_JFFS2_LZO
521         jffs2_lzo_init();
522 #endif
523         return 0;
524 }
525
526 int jffs2_compressors_exit(void)
527 {
528 #ifdef CONFIG_JFFS2_RTIME
529         jffs2_rtime_exit();
530 #endif
531 #ifdef CONFIG_JFFS2_ZLIB
532         jffs2_zlib_exit();
533 #endif
534 #ifdef CONFIG_JFFS2_LZO
535         jffs2_lzo_exit();
536 #endif
537         return 0;
538 }