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.41 2004/06/24 09:51:38 havasi Exp $
18 static spinlock_t jffs2_compressor_list_lock
= SPIN_LOCK_UNLOCKED
;
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 void jffs2_set_compression_mode(int mode
)
28 jffs2_compression_mode
= mode
;
31 int jffs2_get_compression_mode(void)
33 return jffs2_compression_mode
;
36 /* Statistics for blocks stored without compression */
37 static uint32_t none_stat_compr_blocks
=0,none_stat_decompr_blocks
=0,none_stat_compr_size
=0;
40 * @data: Pointer to uncompressed data
41 * @cdata: Pointer to returned pointer to buffer for compressed data
42 * @datalen: On entry, holds the amount of data available for compression.
43 * On exit, expected to hold the amount of data actually compressed.
44 * @cdatalen: On entry, holds the amount of space available for compressed
45 * data. On exit, expected to hold the actual size of the compressed
48 * Returns: Lower byte to be stored with data indicating compression type used.
49 * Zero is used to show that the data could not be compressed - the
50 * compressed version was actually larger than the original.
51 * Upper byte will be used later. (soon)
53 * If the cdata buffer isn't large enough to hold all the uncompressed data,
54 * jffs2_compress should compress as much as will fit, and should set
55 * *datalen accordingly to show the amount of data which were compressed.
57 uint16_t jffs2_compress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
58 unsigned char *data_in
, unsigned char **cpage_out
,
59 uint32_t *datalen
, uint32_t *cdatalen
)
61 int ret
= JFFS2_COMPR_NONE
;
63 struct jffs2_compressor
*this, *best
=NULL
;
64 unsigned char *output_buf
= NULL
, *tmp_buf
;
65 uint32_t orig_slen
, orig_dlen
;
66 uint32_t best_slen
=0, best_dlen
=0;
68 switch (jffs2_compression_mode
) {
69 case JFFS2_COMPR_MODE_NONE
:
71 case JFFS2_COMPR_MODE_PRIORITY
:
72 output_buf
= kmalloc(*cdatalen
,GFP_KERNEL
);
74 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. Compression failed.\n");
78 orig_dlen
= *cdatalen
;
79 spin_lock(&jffs2_compressor_list_lock
);
80 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
81 /* Skip decompress-only backwards-compatibility and disabled modules */
82 if ((!this->compress
)||(this->disabled
))
86 spin_unlock(&jffs2_compressor_list_lock
);
88 *cdatalen
= orig_dlen
;
89 compr_ret
= this->compress(data_in
, output_buf
, datalen
, cdatalen
, NULL
);
90 spin_lock(&jffs2_compressor_list_lock
);
94 this->stat_compr_blocks
++;
95 this->stat_compr_orig_size
+= *datalen
;
96 this->stat_compr_new_size
+= *cdatalen
;
100 spin_unlock(&jffs2_compressor_list_lock
);
101 if (ret
== JFFS2_COMPR_NONE
) kfree(output_buf
);
103 case JFFS2_COMPR_MODE_SIZE
:
104 orig_slen
= *datalen
;
105 orig_dlen
= *cdatalen
;
106 spin_lock(&jffs2_compressor_list_lock
);
107 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
108 /* Skip decompress-only backwards-compatibility and disabled modules */
109 if ((!this->compress
)||(this->disabled
))
111 /* Allocating memory for output buffer if necessary */
112 if ((this->compr_buf_size
<orig_dlen
)&&(this->compr_buf
)) {
113 spin_unlock(&jffs2_compressor_list_lock
);
114 kfree(this->compr_buf
);
115 spin_lock(&jffs2_compressor_list_lock
);
116 this->compr_buf_size
=0;
117 this->compr_buf
=NULL
;
119 if (!this->compr_buf
) {
120 spin_unlock(&jffs2_compressor_list_lock
);
121 tmp_buf
= kmalloc(orig_dlen
,GFP_KERNEL
);
122 spin_lock(&jffs2_compressor_list_lock
);
124 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen
);
128 this->compr_buf
= tmp_buf
;
129 this->compr_buf_size
= orig_dlen
;
133 spin_unlock(&jffs2_compressor_list_lock
);
134 *datalen
= orig_slen
;
135 *cdatalen
= orig_dlen
;
136 compr_ret
= this->compress(data_in
, this->compr_buf
, datalen
, cdatalen
, NULL
);
137 spin_lock(&jffs2_compressor_list_lock
);
140 if ((!best_dlen
)||(best_dlen
>*cdatalen
)) {
141 best_dlen
= *cdatalen
;
142 best_slen
= *datalen
;
148 *cdatalen
= best_dlen
;
149 *datalen
= best_slen
;
150 output_buf
= best
->compr_buf
;
151 best
->compr_buf
= NULL
;
152 best
->compr_buf_size
= 0;
153 best
->stat_compr_blocks
++;
154 best
->stat_compr_orig_size
+= best_slen
;
155 best
->stat_compr_new_size
+= best_dlen
;
158 spin_unlock(&jffs2_compressor_list_lock
);
161 printk(KERN_ERR
"JFFS2: unknow compression mode.\n");
164 if (ret
== JFFS2_COMPR_NONE
) {
165 *cpage_out
= data_in
;
166 *datalen
= *cdatalen
;
167 none_stat_compr_blocks
++;
168 none_stat_compr_size
+= *datalen
;
171 *cpage_out
= output_buf
;
176 int jffs2_decompress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
177 uint16_t comprtype
, unsigned char *cdata_in
,
178 unsigned char *data_out
, uint32_t cdatalen
, uint32_t datalen
)
180 struct jffs2_compressor
*this;
183 switch (comprtype
& 0xff) {
184 case JFFS2_COMPR_NONE
:
185 /* This should be special-cased elsewhere, but we might as well deal with it */
186 memcpy(data_out
, cdata_in
, datalen
);
187 none_stat_decompr_blocks
++;
189 case JFFS2_COMPR_ZERO
:
190 memset(data_out
, 0, datalen
);
193 spin_lock(&jffs2_compressor_list_lock
);
194 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
195 if (comprtype
== this->compr
) {
197 spin_unlock(&jffs2_compressor_list_lock
);
198 ret
= this->decompress(cdata_in
, data_out
, cdatalen
, datalen
, NULL
);
199 spin_lock(&jffs2_compressor_list_lock
);
201 printk(KERN_WARNING
"Decompressor \"%s\" returned %d\n", this->name
, ret
);
204 this->stat_decompr_blocks
++;
207 spin_unlock(&jffs2_compressor_list_lock
);
211 printk(KERN_WARNING
"JFFS2 compression type 0x%02x not avaiable.\n", comprtype
);
212 spin_unlock(&jffs2_compressor_list_lock
);
218 int jffs2_register_compressor(struct jffs2_compressor
*comp
)
220 struct jffs2_compressor
*this;
223 printk(KERN_WARNING
"NULL compressor name at registering JFFS2 compressor. Failed.\n");
226 comp
->compr_buf_size
=0;
227 comp
->compr_buf
=NULL
;
229 comp
->stat_compr_orig_size
=0;
230 comp
->stat_compr_new_size
=0;
231 comp
->stat_compr_blocks
=0;
232 comp
->stat_decompr_blocks
=0;
233 D1(printk(KERN_DEBUG
"Registering JFFS2 compressor \"%s\"\n", comp
->name
));
235 spin_lock(&jffs2_compressor_list_lock
);
237 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
238 if (this->priority
< comp
->priority
) {
239 list_add(&comp
->list
, this->list
.prev
);
243 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
245 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
246 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
249 spin_unlock(&jffs2_compressor_list_lock
);
254 int jffs2_unregister_compressor(struct jffs2_compressor
*comp
)
256 D2(struct jffs2_compressor
*this;)
258 D1(printk(KERN_DEBUG
"Unregistering JFFS2 compressor \"%s\"\n", comp
->name
));
260 spin_lock(&jffs2_compressor_list_lock
);
262 if (comp
->usecount
) {
263 spin_unlock(&jffs2_compressor_list_lock
);
264 printk(KERN_WARNING
"JFFS2: Compressor modul is in use. Unregister failed.\n");
267 list_del(&comp
->list
);
269 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
270 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
272 spin_unlock(&jffs2_compressor_list_lock
);
276 #ifdef CONFIG_JFFS2_PROC
278 #define JFFS2_STAT_BUF_SIZE 16000
280 char *jffs2_list_compressors(void)
282 struct jffs2_compressor
*this;
285 act_buf
= buf
= kmalloc(JFFS2_STAT_BUF_SIZE
,GFP_KERNEL
);
286 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
287 act_buf
+= sprintf(act_buf
, "%10s priority:%d ", this->name
, this->priority
);
288 if ((this->disabled
)||(!this->compress
))
289 act_buf
+= sprintf(act_buf
,"disabled");
291 act_buf
+= sprintf(act_buf
,"enabled");
292 act_buf
+= sprintf(act_buf
,"\n");
297 char *jffs2_stats(void)
299 struct jffs2_compressor
*this;
302 act_buf
= buf
= kmalloc(JFFS2_STAT_BUF_SIZE
,GFP_KERNEL
);
304 act_buf
+= sprintf(act_buf
,"JFFS2 compressor statistics:\n");
305 act_buf
+= sprintf(act_buf
,"%10s ","none");
306 act_buf
+= sprintf(act_buf
,"compr: %d blocks (%d) decompr: %d blocks\n", none_stat_compr_blocks
,
307 none_stat_compr_size
, none_stat_decompr_blocks
);
308 spin_lock(&jffs2_compressor_list_lock
);
309 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
310 act_buf
+= sprintf(act_buf
,"%10s ",this->name
);
311 if ((this->disabled
)||(!this->compress
))
312 act_buf
+= sprintf(act_buf
,"- ");
314 act_buf
+= sprintf(act_buf
,"+ ");
315 act_buf
+= sprintf(act_buf
,"compr: %d blocks (%d/%d) decompr: %d blocks ", this->stat_compr_blocks
,
316 this->stat_compr_new_size
, this->stat_compr_orig_size
,
317 this->stat_decompr_blocks
);
318 act_buf
+= sprintf(act_buf
,"\n");
320 spin_unlock(&jffs2_compressor_list_lock
);
325 char *jffs2_get_compression_mode_name(void)
327 switch (jffs2_compression_mode
) {
328 case JFFS2_COMPR_MODE_NONE
:
330 case JFFS2_COMPR_MODE_PRIORITY
:
332 case JFFS2_COMPR_MODE_SIZE
:
338 int jffs2_set_compression_mode_name(const char *name
)
340 if (!strcmp("none",name
)) {
341 jffs2_compression_mode
= JFFS2_COMPR_MODE_NONE
;
344 if (!strcmp("priority",name
)) {
345 jffs2_compression_mode
= JFFS2_COMPR_MODE_PRIORITY
;
348 if (!strcmp("size",name
)) {
349 jffs2_compression_mode
= JFFS2_COMPR_MODE_SIZE
;
355 static int jffs2_compressor_Xable(const char *name
, int disabled
)
357 struct jffs2_compressor
*this;
358 spin_lock(&jffs2_compressor_list_lock
);
359 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
360 if (!strcmp(this->name
, name
)) {
361 this->disabled
= disabled
;
362 spin_unlock(&jffs2_compressor_list_lock
);
366 spin_unlock(&jffs2_compressor_list_lock
);
367 printk(KERN_WARNING
"JFFS2: compressor %s not found.\n",name
);
371 int jffs2_enable_compressor_name(const char *name
)
373 return jffs2_compressor_Xable(name
, 0);
376 int jffs2_disable_compressor_name(const char *name
)
378 return jffs2_compressor_Xable(name
, 1);
381 int jffs2_set_compressor_priority(const char *name
, int priority
)
383 struct jffs2_compressor
*this,*comp
;
384 spin_lock(&jffs2_compressor_list_lock
);
385 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
386 if (!strcmp(this->name
, name
)) {
387 this->priority
= priority
;
392 spin_unlock(&jffs2_compressor_list_lock
);
393 printk(KERN_WARNING
"JFFS2: compressor %s not found.\n",name
);
396 /* list is sorted in the order of priority, so if
397 we change it we have to reinsert it into the
399 list_del(&comp
->list
);
400 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
401 if (this->priority
< comp
->priority
) {
402 list_add(&comp
->list
, this->list
.prev
);
403 spin_unlock(&jffs2_compressor_list_lock
);
407 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
408 spin_unlock(&jffs2_compressor_list_lock
);
414 void jffs2_free_comprbuf(unsigned char *comprbuf
, unsigned char *orig
)
416 if (orig
!= comprbuf
)
420 int jffs2_compressors_init(void)
422 /* Registering compressors */
423 #ifdef CONFIG_JFFS2_ZLIB
426 #ifdef CONFIG_JFFS2_RTIME
429 #ifdef CONFIG_JFFS2_RUBIN
430 jffs2_rubinmips_init();
431 jffs2_dynrubin_init();
433 #ifdef CONFIG_JFFS2_LZARI
436 #ifdef CONFIG_JFFS2_LZO
439 /* Setting default compression mode */
440 #ifdef CONFIG_JFFS2_CMODE_NONE
441 jffs2_compression_mode
= JFFS2_COMPR_MODE_NONE
;
442 D1(printk(KERN_INFO
"JFFS2: default compression mode: none\n");)
444 #ifdef CONFIG_JFFS2_CMODE_SIZE
445 jffs2_compression_mode
= JFFS2_COMPR_MODE_SIZE
;
446 D1(printk(KERN_INFO
"JFFS2: default compression mode: size\n");)
448 D1(printk(KERN_INFO
"JFFS2: default compression mode: priority\n");)
454 int jffs2_compressors_exit(void)
456 /* Unregistering compressors */
457 #ifdef CONFIG_JFFS2_LZO
460 #ifdef CONFIG_JFFS2_LZARI
463 #ifdef CONFIG_JFFS2_RUBIN
464 jffs2_dynrubin_exit();
465 jffs2_rubinmips_exit();
467 #ifdef CONFIG_JFFS2_RTIME
470 #ifdef CONFIG_JFFS2_ZLIB