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
)
92 case JFFS2_COMPR_MODE_SIZE
:
94 orig_dlen
= *cdatalen
;
95 spin_lock(&jffs2_compressor_list_lock
);
96 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
97 /* Skip decompress-only backwards-compatibility and disabled modules */
98 if ((!this->compress
)||(this->disabled
))
100 /* Allocating memory for output buffer if necessary */
101 if ((this->compr_buf_size
<orig_dlen
)&&(this->compr_buf
)) {
102 spin_unlock(&jffs2_compressor_list_lock
);
103 kfree(this->compr_buf
);
104 spin_lock(&jffs2_compressor_list_lock
);
105 this->compr_buf_size
=0;
106 this->compr_buf
=NULL
;
108 if (!this->compr_buf
) {
109 spin_unlock(&jffs2_compressor_list_lock
);
110 tmp_buf
= kmalloc(orig_dlen
,GFP_KERNEL
);
111 spin_lock(&jffs2_compressor_list_lock
);
113 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen
);
117 this->compr_buf
= tmp_buf
;
118 this->compr_buf_size
= orig_dlen
;
122 spin_unlock(&jffs2_compressor_list_lock
);
123 *datalen
= orig_slen
;
124 *cdatalen
= orig_dlen
;
125 compr_ret
= this->compress(data_in
, this->compr_buf
, datalen
, cdatalen
, NULL
);
126 spin_lock(&jffs2_compressor_list_lock
);
129 if ((!best_dlen
)||(best_dlen
>*cdatalen
)) {
130 best_dlen
= *cdatalen
;
131 best_slen
= *datalen
;
137 *cdatalen
= best_dlen
;
138 *datalen
= best_slen
;
139 output_buf
= best
->compr_buf
;
140 best
->compr_buf
= NULL
;
141 best
->compr_buf_size
= 0;
142 best
->stat_compr_blocks
++;
143 best
->stat_compr_orig_size
+= best_slen
;
144 best
->stat_compr_new_size
+= best_dlen
;
147 spin_unlock(&jffs2_compressor_list_lock
);
150 printk(KERN_ERR
"JFFS2: unknow compression mode.\n");
153 if (ret
== JFFS2_COMPR_NONE
) {
154 *cpage_out
= data_in
;
155 *datalen
= *cdatalen
;
156 none_stat_compr_blocks
++;
157 none_stat_compr_size
+= *datalen
;
160 *cpage_out
= output_buf
;
165 int jffs2_decompress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
166 uint16_t comprtype
, unsigned char *cdata_in
,
167 unsigned char *data_out
, uint32_t cdatalen
, uint32_t datalen
)
169 struct jffs2_compressor
*this;
172 /* Older code had a bug where it would write non-zero 'usercompr'
173 fields. Deal with it. */
174 if ((comprtype
& 0xff) <= JFFS2_COMPR_ZLIB
)
177 switch (comprtype
& 0xff) {
178 case JFFS2_COMPR_NONE
:
179 /* This should be special-cased elsewhere, but we might as well deal with it */
180 memcpy(data_out
, cdata_in
, datalen
);
181 none_stat_decompr_blocks
++;
183 case JFFS2_COMPR_ZERO
:
184 memset(data_out
, 0, datalen
);
187 spin_lock(&jffs2_compressor_list_lock
);
188 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
189 if (comprtype
== this->compr
) {
191 spin_unlock(&jffs2_compressor_list_lock
);
192 ret
= this->decompress(cdata_in
, data_out
, cdatalen
, datalen
, NULL
);
193 spin_lock(&jffs2_compressor_list_lock
);
195 printk(KERN_WARNING
"Decompressor \"%s\" returned %d\n", this->name
, ret
);
198 this->stat_decompr_blocks
++;
201 spin_unlock(&jffs2_compressor_list_lock
);
205 printk(KERN_WARNING
"JFFS2 compression type 0x%02x not available.\n", comprtype
);
206 spin_unlock(&jffs2_compressor_list_lock
);
212 int jffs2_register_compressor(struct jffs2_compressor
*comp
)
214 struct jffs2_compressor
*this;
217 printk(KERN_WARNING
"NULL compressor name at registering JFFS2 compressor. Failed.\n");
220 comp
->compr_buf_size
=0;
221 comp
->compr_buf
=NULL
;
223 comp
->stat_compr_orig_size
=0;
224 comp
->stat_compr_new_size
=0;
225 comp
->stat_compr_blocks
=0;
226 comp
->stat_decompr_blocks
=0;
227 D1(printk(KERN_DEBUG
"Registering JFFS2 compressor \"%s\"\n", comp
->name
));
229 spin_lock(&jffs2_compressor_list_lock
);
231 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
232 if (this->priority
< comp
->priority
) {
233 list_add(&comp
->list
, this->list
.prev
);
237 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
239 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
240 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
243 spin_unlock(&jffs2_compressor_list_lock
);
248 int jffs2_unregister_compressor(struct jffs2_compressor
*comp
)
250 D2(struct jffs2_compressor
*this;)
252 D1(printk(KERN_DEBUG
"Unregistering JFFS2 compressor \"%s\"\n", comp
->name
));
254 spin_lock(&jffs2_compressor_list_lock
);
256 if (comp
->usecount
) {
257 spin_unlock(&jffs2_compressor_list_lock
);
258 printk(KERN_WARNING
"JFFS2: Compressor modul is in use. Unregister failed.\n");
261 list_del(&comp
->list
);
263 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
264 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
266 spin_unlock(&jffs2_compressor_list_lock
);
270 void jffs2_free_comprbuf(unsigned char *comprbuf
, unsigned char *orig
)
272 if (orig
!= comprbuf
)
276 int __init
jffs2_compressors_init(void)
278 /* Registering compressors */
279 #ifdef CONFIG_JFFS2_ZLIB
282 #ifdef CONFIG_JFFS2_RTIME
285 #ifdef CONFIG_JFFS2_RUBIN
286 jffs2_rubinmips_init();
287 jffs2_dynrubin_init();
289 /* Setting default compression mode */
290 #ifdef CONFIG_JFFS2_CMODE_NONE
291 jffs2_compression_mode
= JFFS2_COMPR_MODE_NONE
;
292 D1(printk(KERN_INFO
"JFFS2: default compression mode: none\n");)
294 #ifdef CONFIG_JFFS2_CMODE_SIZE
295 jffs2_compression_mode
= JFFS2_COMPR_MODE_SIZE
;
296 D1(printk(KERN_INFO
"JFFS2: default compression mode: size\n");)
298 D1(printk(KERN_INFO
"JFFS2: default compression mode: priority\n");)
304 int jffs2_compressors_exit(void)
306 /* Unregistering compressors */
307 #ifdef CONFIG_JFFS2_RUBIN
308 jffs2_dynrubin_exit();
309 jffs2_rubinmips_exit();
311 #ifdef CONFIG_JFFS2_RTIME
314 #ifdef CONFIG_JFFS2_ZLIB