2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Created by Arjan van de Ven <arjanv@redhat.com>
7 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
8 * University of Szeged, Hungary
10 * For licensing information, see the file 'LICENCE' in this directory.
16 static DEFINE_SPINLOCK(jffs2_compressor_list_lock
);
18 /* Available compressors are on this list */
19 static LIST_HEAD(jffs2_compressor_list
);
21 /* Actual compression mode */
22 static int jffs2_compression_mode
= JFFS2_COMPR_MODE_PRIORITY
;
24 /* Statistics for blocks stored without compression */
25 static uint32_t none_stat_compr_blocks
=0,none_stat_decompr_blocks
=0,none_stat_compr_size
=0;
28 * @data: Pointer to uncompressed data
29 * @cdata: Pointer to returned pointer to buffer for compressed data
30 * @datalen: On entry, holds the amount of data available for compression.
31 * On exit, expected to hold the amount of data actually compressed.
32 * @cdatalen: On entry, holds the amount of space available for compressed
33 * data. On exit, expected to hold the actual size of the compressed
36 * Returns: Lower byte to be stored with data indicating compression type used.
37 * Zero is used to show that the data could not be compressed - the
38 * compressed version was actually larger than the original.
39 * Upper byte will be used later. (soon)
41 * If the cdata buffer isn't large enough to hold all the uncompressed data,
42 * jffs2_compress should compress as much as will fit, and should set
43 * *datalen accordingly to show the amount of data which were compressed.
45 uint16_t jffs2_compress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
46 unsigned char *data_in
, unsigned char **cpage_out
,
47 uint32_t *datalen
, uint32_t *cdatalen
)
49 int ret
= JFFS2_COMPR_NONE
;
51 struct jffs2_compressor
*this, *best
=NULL
;
52 unsigned char *output_buf
= NULL
, *tmp_buf
;
53 uint32_t orig_slen
, orig_dlen
;
54 uint32_t best_slen
=0, best_dlen
=0;
56 switch (jffs2_compression_mode
) {
57 case JFFS2_COMPR_MODE_NONE
:
59 case JFFS2_COMPR_MODE_PRIORITY
:
60 output_buf
= kmalloc(*cdatalen
,GFP_KERNEL
);
62 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. Compression failed.\n");
66 orig_dlen
= *cdatalen
;
67 spin_lock(&jffs2_compressor_list_lock
);
68 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
69 /* Skip decompress-only backwards-compatibility and disabled modules */
70 if ((!this->compress
)||(this->disabled
))
74 spin_unlock(&jffs2_compressor_list_lock
);
76 *cdatalen
= orig_dlen
;
77 compr_ret
= this->compress(data_in
, output_buf
, datalen
, cdatalen
, NULL
);
78 spin_lock(&jffs2_compressor_list_lock
);
82 this->stat_compr_blocks
++;
83 this->stat_compr_orig_size
+= *datalen
;
84 this->stat_compr_new_size
+= *cdatalen
;
88 spin_unlock(&jffs2_compressor_list_lock
);
89 if (ret
== JFFS2_COMPR_NONE
) kfree(output_buf
);
91 case JFFS2_COMPR_MODE_SIZE
:
93 orig_dlen
= *cdatalen
;
94 spin_lock(&jffs2_compressor_list_lock
);
95 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
96 /* Skip decompress-only backwards-compatibility and disabled modules */
97 if ((!this->compress
)||(this->disabled
))
99 /* Allocating memory for output buffer if necessary */
100 if ((this->compr_buf_size
<orig_dlen
)&&(this->compr_buf
)) {
101 spin_unlock(&jffs2_compressor_list_lock
);
102 kfree(this->compr_buf
);
103 spin_lock(&jffs2_compressor_list_lock
);
104 this->compr_buf_size
=0;
105 this->compr_buf
=NULL
;
107 if (!this->compr_buf
) {
108 spin_unlock(&jffs2_compressor_list_lock
);
109 tmp_buf
= kmalloc(orig_dlen
,GFP_KERNEL
);
110 spin_lock(&jffs2_compressor_list_lock
);
112 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen
);
116 this->compr_buf
= tmp_buf
;
117 this->compr_buf_size
= orig_dlen
;
121 spin_unlock(&jffs2_compressor_list_lock
);
122 *datalen
= orig_slen
;
123 *cdatalen
= orig_dlen
;
124 compr_ret
= this->compress(data_in
, this->compr_buf
, datalen
, cdatalen
, NULL
);
125 spin_lock(&jffs2_compressor_list_lock
);
128 if ((!best_dlen
)||(best_dlen
>*cdatalen
)) {
129 best_dlen
= *cdatalen
;
130 best_slen
= *datalen
;
136 *cdatalen
= best_dlen
;
137 *datalen
= best_slen
;
138 output_buf
= best
->compr_buf
;
139 best
->compr_buf
= NULL
;
140 best
->compr_buf_size
= 0;
141 best
->stat_compr_blocks
++;
142 best
->stat_compr_orig_size
+= best_slen
;
143 best
->stat_compr_new_size
+= best_dlen
;
146 spin_unlock(&jffs2_compressor_list_lock
);
149 printk(KERN_ERR
"JFFS2: unknow compression mode.\n");
152 if (ret
== JFFS2_COMPR_NONE
) {
153 *cpage_out
= data_in
;
154 *datalen
= *cdatalen
;
155 none_stat_compr_blocks
++;
156 none_stat_compr_size
+= *datalen
;
159 *cpage_out
= output_buf
;
164 int jffs2_decompress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
165 uint16_t comprtype
, unsigned char *cdata_in
,
166 unsigned char *data_out
, uint32_t cdatalen
, uint32_t datalen
)
168 struct jffs2_compressor
*this;
171 /* Older code had a bug where it would write non-zero 'usercompr'
172 fields. Deal with it. */
173 if ((comprtype
& 0xff) <= JFFS2_COMPR_ZLIB
)
176 switch (comprtype
& 0xff) {
177 case JFFS2_COMPR_NONE
:
178 /* This should be special-cased elsewhere, but we might as well deal with it */
179 memcpy(data_out
, cdata_in
, datalen
);
180 none_stat_decompr_blocks
++;
182 case JFFS2_COMPR_ZERO
:
183 memset(data_out
, 0, datalen
);
186 spin_lock(&jffs2_compressor_list_lock
);
187 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
188 if (comprtype
== this->compr
) {
190 spin_unlock(&jffs2_compressor_list_lock
);
191 ret
= this->decompress(cdata_in
, data_out
, cdatalen
, datalen
, NULL
);
192 spin_lock(&jffs2_compressor_list_lock
);
194 printk(KERN_WARNING
"Decompressor \"%s\" returned %d\n", this->name
, ret
);
197 this->stat_decompr_blocks
++;
200 spin_unlock(&jffs2_compressor_list_lock
);
204 printk(KERN_WARNING
"JFFS2 compression type 0x%02x not available.\n", comprtype
);
205 spin_unlock(&jffs2_compressor_list_lock
);
211 int jffs2_register_compressor(struct jffs2_compressor
*comp
)
213 struct jffs2_compressor
*this;
216 printk(KERN_WARNING
"NULL compressor name at registering JFFS2 compressor. Failed.\n");
219 comp
->compr_buf_size
=0;
220 comp
->compr_buf
=NULL
;
222 comp
->stat_compr_orig_size
=0;
223 comp
->stat_compr_new_size
=0;
224 comp
->stat_compr_blocks
=0;
225 comp
->stat_decompr_blocks
=0;
226 D1(printk(KERN_DEBUG
"Registering JFFS2 compressor \"%s\"\n", comp
->name
));
228 spin_lock(&jffs2_compressor_list_lock
);
230 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
231 if (this->priority
< comp
->priority
) {
232 list_add(&comp
->list
, this->list
.prev
);
236 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
238 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
239 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
242 spin_unlock(&jffs2_compressor_list_lock
);
247 int jffs2_unregister_compressor(struct jffs2_compressor
*comp
)
249 D2(struct jffs2_compressor
*this;)
251 D1(printk(KERN_DEBUG
"Unregistering JFFS2 compressor \"%s\"\n", comp
->name
));
253 spin_lock(&jffs2_compressor_list_lock
);
255 if (comp
->usecount
) {
256 spin_unlock(&jffs2_compressor_list_lock
);
257 printk(KERN_WARNING
"JFFS2: Compressor modul is in use. Unregister failed.\n");
260 list_del(&comp
->list
);
262 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
263 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
265 spin_unlock(&jffs2_compressor_list_lock
);
269 void jffs2_free_comprbuf(unsigned char *comprbuf
, unsigned char *orig
)
271 if (orig
!= comprbuf
)
275 int __init
jffs2_compressors_init(void)
277 /* Registering compressors */
278 #ifdef CONFIG_JFFS2_ZLIB
281 #ifdef CONFIG_JFFS2_RTIME
284 #ifdef CONFIG_JFFS2_RUBIN
285 jffs2_rubinmips_init();
286 jffs2_dynrubin_init();
288 /* Setting default compression mode */
289 #ifdef CONFIG_JFFS2_CMODE_NONE
290 jffs2_compression_mode
= JFFS2_COMPR_MODE_NONE
;
291 D1(printk(KERN_INFO
"JFFS2: default compression mode: none\n");)
293 #ifdef CONFIG_JFFS2_CMODE_SIZE
294 jffs2_compression_mode
= JFFS2_COMPR_MODE_SIZE
;
295 D1(printk(KERN_INFO
"JFFS2: default compression mode: size\n");)
297 D1(printk(KERN_INFO
"JFFS2: default compression mode: priority\n");)
303 int jffs2_compressors_exit(void)
305 /* Unregistering compressors */
306 #ifdef CONFIG_JFFS2_RUBIN
307 jffs2_dynrubin_exit();
308 jffs2_rubinmips_exit();
310 #ifdef CONFIG_JFFS2_RTIME
313 #ifdef CONFIG_JFFS2_ZLIB