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);//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;
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
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)
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.
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
;
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
:
62 case JFFS2_COMPR_MODE_PRIORITY
:
63 output_buf
= kmalloc(*cdatalen
,GFP_KERNEL
);
65 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. Compression failed.\n");
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
))
77 spin_unlock(&jffs2_compressor_list_lock
);
79 *cdatalen
= orig_dlen
;
80 compr_ret
= this->compress(data_in
, output_buf
, datalen
, cdatalen
, NULL
);
81 spin_lock(&jffs2_compressor_list_lock
);
85 this->stat_compr_blocks
++;
86 this->stat_compr_orig_size
+= *datalen
;
87 this->stat_compr_new_size
+= *cdatalen
;
91 spin_unlock(&jffs2_compressor_list_lock
);
92 if (ret
== JFFS2_COMPR_NONE
) kfree(output_buf
);
94 case JFFS2_COMPR_MODE_SIZE
:
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
))
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
;
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
);
115 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen
);
119 this->compr_buf
= tmp_buf
;
120 this->compr_buf_size
= orig_dlen
;
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
);
131 if ((!best_dlen
)||(best_dlen
>*cdatalen
)) {
132 best_dlen
= *cdatalen
;
133 best_slen
= *datalen
;
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
;
149 spin_unlock(&jffs2_compressor_list_lock
);
152 printk(KERN_ERR
"JFFS2: unknow compression mode.\n");
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
;
162 *cpage_out
= output_buf
;
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;
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
)
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
++;
185 case JFFS2_COMPR_ZERO
:
186 memset(data_out
, 0, datalen
);
189 spin_lock(&jffs2_compressor_list_lock
);
190 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
191 if (comprtype
== this->compr
) {
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
);
197 printk(KERN_WARNING
"Decompressor \"%s\" returned %d\n", this->name
, ret
);
200 this->stat_decompr_blocks
++;
203 spin_unlock(&jffs2_compressor_list_lock
);
207 printk(KERN_WARNING
"JFFS2 compression type 0x%02x not available.\n", comprtype
);
208 spin_unlock(&jffs2_compressor_list_lock
);
214 int jffs2_register_compressor(struct jffs2_compressor
*comp
)
216 struct jffs2_compressor
*this;
219 printk(KERN_WARNING
"NULL compressor name at registering JFFS2 compressor. Failed.\n");
222 comp
->compr_buf_size
=0;
223 comp
->compr_buf
=NULL
;
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
);
239 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
241 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
242 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
245 spin_unlock(&jffs2_compressor_list_lock
);
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");
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
);
268 spin_unlock(&jffs2_compressor_list_lock
);
272 #ifdef CONFIG_JFFS2_PROC
274 #define JFFS2_STAT_BUF_SIZE 16000
276 char *jffs2_list_compressors(void)
278 struct jffs2_compressor
*this;
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");
287 act_buf
+= sprintf(act_buf
,"enabled");
288 act_buf
+= sprintf(act_buf
,"\n");
293 char *jffs2_stats(void)
295 struct jffs2_compressor
*this;
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
,"- ");
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");
316 spin_unlock(&jffs2_compressor_list_lock
);
321 char *jffs2_get_compression_mode_name(void)
323 switch (jffs2_compression_mode
) {
324 case JFFS2_COMPR_MODE_NONE
:
326 case JFFS2_COMPR_MODE_PRIORITY
:
328 case JFFS2_COMPR_MODE_SIZE
:
334 int jffs2_set_compression_mode_name(const char *name
)
336 if (!strcmp("none",name
)) {
337 jffs2_compression_mode
= JFFS2_COMPR_MODE_NONE
;
340 if (!strcmp("priority",name
)) {
341 jffs2_compression_mode
= JFFS2_COMPR_MODE_PRIORITY
;
344 if (!strcmp("size",name
)) {
345 jffs2_compression_mode
= JFFS2_COMPR_MODE_SIZE
;
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
);
362 spin_unlock(&jffs2_compressor_list_lock
);
363 printk(KERN_WARNING
"JFFS2: compressor %s not found.\n",name
);
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
;
388 spin_unlock(&jffs2_compressor_list_lock
);
389 printk(KERN_WARNING
"JFFS2: compressor %s not found.\n",name
);
392 /* list is sorted in the order of priority, so if
393 we change it we have to reinsert it into the
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
);
403 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
404 spin_unlock(&jffs2_compressor_list_lock
);
410 void jffs2_free_comprbuf(unsigned char *comprbuf
, unsigned char *orig
)
412 if (orig
!= comprbuf
)
416 int jffs2_compressors_init(void)
418 /* Registering compressors */
419 #ifdef CONFIG_JFFS2_ZLIB
422 #ifdef CONFIG_JFFS2_RTIME
425 #ifdef CONFIG_JFFS2_RUBIN
426 jffs2_rubinmips_init();
427 jffs2_dynrubin_init();
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");)
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");)
438 D1(printk(KERN_INFO
"JFFS2: default compression mode: priority\n");)
444 int jffs2_compressors_exit(void)
446 /* Unregistering compressors */
447 #ifdef CONFIG_JFFS2_RUBIN
448 jffs2_dynrubin_exit();
449 jffs2_rubinmips_exit();
451 #ifdef CONFIG_JFFS2_RTIME
454 #ifdef CONFIG_JFFS2_ZLIB