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
);
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 /* Statistics for blocks stored without compression */
27 static uint32_t none_stat_compr_blocks
=0,none_stat_decompr_blocks
=0,none_stat_compr_size
=0;
30 * @data: Pointer to uncompressed data
31 * @cdata: Pointer to returned pointer to buffer for compressed data
32 * @datalen: On entry, holds the amount of data available for compression.
33 * On exit, expected to hold the amount of data actually compressed.
34 * @cdatalen: On entry, holds the amount of space available for compressed
35 * data. On exit, expected to hold the actual size of the compressed
38 * Returns: Lower byte to be stored with data indicating compression type used.
39 * Zero is used to show that the data could not be compressed - the
40 * compressed version was actually larger than the original.
41 * Upper byte will be used later. (soon)
43 * If the cdata buffer isn't large enough to hold all the uncompressed data,
44 * jffs2_compress should compress as much as will fit, and should set
45 * *datalen accordingly to show the amount of data which were compressed.
47 uint16_t jffs2_compress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
48 unsigned char *data_in
, unsigned char **cpage_out
,
49 uint32_t *datalen
, uint32_t *cdatalen
)
51 int ret
= JFFS2_COMPR_NONE
;
53 struct jffs2_compressor
*this, *best
=NULL
;
54 unsigned char *output_buf
= NULL
, *tmp_buf
;
55 uint32_t orig_slen
, orig_dlen
;
56 uint32_t best_slen
=0, best_dlen
=0;
58 switch (jffs2_compression_mode
) {
59 case JFFS2_COMPR_MODE_NONE
:
61 case JFFS2_COMPR_MODE_PRIORITY
:
62 output_buf
= kmalloc(*cdatalen
,GFP_KERNEL
);
64 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. Compression failed.\n");
68 orig_dlen
= *cdatalen
;
69 spin_lock(&jffs2_compressor_list_lock
);
70 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
71 /* Skip decompress-only backwards-compatibility and disabled modules */
72 if ((!this->compress
)||(this->disabled
))
76 spin_unlock(&jffs2_compressor_list_lock
);
78 *cdatalen
= orig_dlen
;
79 compr_ret
= this->compress(data_in
, output_buf
, datalen
, cdatalen
, NULL
);
80 spin_lock(&jffs2_compressor_list_lock
);
84 this->stat_compr_blocks
++;
85 this->stat_compr_orig_size
+= *datalen
;
86 this->stat_compr_new_size
+= *cdatalen
;
90 spin_unlock(&jffs2_compressor_list_lock
);
91 if (ret
== JFFS2_COMPR_NONE
) kfree(output_buf
);
93 case JFFS2_COMPR_MODE_SIZE
:
95 orig_dlen
= *cdatalen
;
96 spin_lock(&jffs2_compressor_list_lock
);
97 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
98 /* Skip decompress-only backwards-compatibility and disabled modules */
99 if ((!this->compress
)||(this->disabled
))
101 /* Allocating memory for output buffer if necessary */
102 if ((this->compr_buf_size
<orig_dlen
)&&(this->compr_buf
)) {
103 spin_unlock(&jffs2_compressor_list_lock
);
104 kfree(this->compr_buf
);
105 spin_lock(&jffs2_compressor_list_lock
);
106 this->compr_buf_size
=0;
107 this->compr_buf
=NULL
;
109 if (!this->compr_buf
) {
110 spin_unlock(&jffs2_compressor_list_lock
);
111 tmp_buf
= kmalloc(orig_dlen
,GFP_KERNEL
);
112 spin_lock(&jffs2_compressor_list_lock
);
114 printk(KERN_WARNING
"JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen
);
118 this->compr_buf
= tmp_buf
;
119 this->compr_buf_size
= orig_dlen
;
123 spin_unlock(&jffs2_compressor_list_lock
);
124 *datalen
= orig_slen
;
125 *cdatalen
= orig_dlen
;
126 compr_ret
= this->compress(data_in
, this->compr_buf
, datalen
, cdatalen
, NULL
);
127 spin_lock(&jffs2_compressor_list_lock
);
130 if ((!best_dlen
)||(best_dlen
>*cdatalen
)) {
131 best_dlen
= *cdatalen
;
132 best_slen
= *datalen
;
138 *cdatalen
= best_dlen
;
139 *datalen
= best_slen
;
140 output_buf
= best
->compr_buf
;
141 best
->compr_buf
= NULL
;
142 best
->compr_buf_size
= 0;
143 best
->stat_compr_blocks
++;
144 best
->stat_compr_orig_size
+= best_slen
;
145 best
->stat_compr_new_size
+= best_dlen
;
148 spin_unlock(&jffs2_compressor_list_lock
);
151 printk(KERN_ERR
"JFFS2: unknow compression mode.\n");
154 if (ret
== JFFS2_COMPR_NONE
) {
155 *cpage_out
= data_in
;
156 *datalen
= *cdatalen
;
157 none_stat_compr_blocks
++;
158 none_stat_compr_size
+= *datalen
;
161 *cpage_out
= output_buf
;
166 int jffs2_decompress(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
167 uint16_t comprtype
, unsigned char *cdata_in
,
168 unsigned char *data_out
, uint32_t cdatalen
, uint32_t datalen
)
170 struct jffs2_compressor
*this;
173 /* Older code had a bug where it would write non-zero 'usercompr'
174 fields. Deal with it. */
175 if ((comprtype
& 0xff) <= JFFS2_COMPR_ZLIB
)
178 switch (comprtype
& 0xff) {
179 case JFFS2_COMPR_NONE
:
180 /* This should be special-cased elsewhere, but we might as well deal with it */
181 memcpy(data_out
, cdata_in
, datalen
);
182 none_stat_decompr_blocks
++;
184 case JFFS2_COMPR_ZERO
:
185 memset(data_out
, 0, datalen
);
188 spin_lock(&jffs2_compressor_list_lock
);
189 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
190 if (comprtype
== this->compr
) {
192 spin_unlock(&jffs2_compressor_list_lock
);
193 ret
= this->decompress(cdata_in
, data_out
, cdatalen
, datalen
, NULL
);
194 spin_lock(&jffs2_compressor_list_lock
);
196 printk(KERN_WARNING
"Decompressor \"%s\" returned %d\n", this->name
, ret
);
199 this->stat_decompr_blocks
++;
202 spin_unlock(&jffs2_compressor_list_lock
);
206 printk(KERN_WARNING
"JFFS2 compression type 0x%02x not available.\n", comprtype
);
207 spin_unlock(&jffs2_compressor_list_lock
);
213 int jffs2_register_compressor(struct jffs2_compressor
*comp
)
215 struct jffs2_compressor
*this;
218 printk(KERN_WARNING
"NULL compressor name at registering JFFS2 compressor. Failed.\n");
221 comp
->compr_buf_size
=0;
222 comp
->compr_buf
=NULL
;
224 comp
->stat_compr_orig_size
=0;
225 comp
->stat_compr_new_size
=0;
226 comp
->stat_compr_blocks
=0;
227 comp
->stat_decompr_blocks
=0;
228 D1(printk(KERN_DEBUG
"Registering JFFS2 compressor \"%s\"\n", comp
->name
));
230 spin_lock(&jffs2_compressor_list_lock
);
232 list_for_each_entry(this, &jffs2_compressor_list
, list
) {
233 if (this->priority
< comp
->priority
) {
234 list_add(&comp
->list
, this->list
.prev
);
238 list_add_tail(&comp
->list
, &jffs2_compressor_list
);
240 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
241 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
244 spin_unlock(&jffs2_compressor_list_lock
);
249 int jffs2_unregister_compressor(struct jffs2_compressor
*comp
)
251 D2(struct jffs2_compressor
*this;)
253 D1(printk(KERN_DEBUG
"Unregistering JFFS2 compressor \"%s\"\n", comp
->name
));
255 spin_lock(&jffs2_compressor_list_lock
);
257 if (comp
->usecount
) {
258 spin_unlock(&jffs2_compressor_list_lock
);
259 printk(KERN_WARNING
"JFFS2: Compressor modul is in use. Unregister failed.\n");
262 list_del(&comp
->list
);
264 D2(list_for_each_entry(this, &jffs2_compressor_list
, list
) {
265 printk(KERN_DEBUG
"Compressor \"%s\", prio %d\n", this->name
, this->priority
);
267 spin_unlock(&jffs2_compressor_list_lock
);
271 void jffs2_free_comprbuf(unsigned char *comprbuf
, unsigned char *orig
)
273 if (orig
!= comprbuf
)
277 int __init
jffs2_compressors_init(void)
279 /* Registering compressors */
280 #ifdef CONFIG_JFFS2_ZLIB
283 #ifdef CONFIG_JFFS2_RTIME
286 #ifdef CONFIG_JFFS2_RUBIN
287 jffs2_rubinmips_init();
288 jffs2_dynrubin_init();
290 /* Setting default compression mode */
291 #ifdef CONFIG_JFFS2_CMODE_NONE
292 jffs2_compression_mode
= JFFS2_COMPR_MODE_NONE
;
293 D1(printk(KERN_INFO
"JFFS2: default compression mode: none\n");)
295 #ifdef CONFIG_JFFS2_CMODE_SIZE
296 jffs2_compression_mode
= JFFS2_COMPR_MODE_SIZE
;
297 D1(printk(KERN_INFO
"JFFS2: default compression mode: size\n");)
299 D1(printk(KERN_INFO
"JFFS2: default compression mode: priority\n");)
305 int jffs2_compressors_exit(void)
307 /* Unregistering compressors */
308 #ifdef CONFIG_JFFS2_RUBIN
309 jffs2_dynrubin_exit();
310 jffs2_rubinmips_exit();
312 #ifdef CONFIG_JFFS2_RTIME
315 #ifdef CONFIG_JFFS2_ZLIB