blob: d90ca05e4992302f698ab3e99df6f682013f7182 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Created by Arjan van de Ven <arjanv@redhat.com>
6 *
David Woodhousec00c3102007-04-25 14:16:47 +01007 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
David Woodhouseef53cb02007-07-10 10:01:22 +01008 * University of Szeged, Hungary
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * For licensing information, see the file 'LICENCE' in this directory.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include "compr.h"
15
16static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
17
18/* Available compressors are on this list */
19static LIST_HEAD(jffs2_compressor_list);
20
21/* Actual compression mode */
22static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
23
24/* Statistics for blocks stored without compression */
25static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
26
27/* jffs2_compress:
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
34 * data.
35 *
36 * Returns: Lower byte to be stored with data indicating compression type used.
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000037 * Zero is used to show that the data could not be compressed - the
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * compressed version was actually larger than the original.
39 * Upper byte will be used later. (soon)
40 *
41 * If the cdata buffer isn't large enough to hold all the uncompressed data,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000042 * jffs2_compress should compress as much as will fit, and should set
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * *datalen accordingly to show the amount of data which were compressed.
44 */
45uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
David Woodhouseef53cb02007-07-10 10:01:22 +010046 unsigned char *data_in, unsigned char **cpage_out,
47 uint32_t *datalen, uint32_t *cdatalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int ret = JFFS2_COMPR_NONE;
David Woodhouseef53cb02007-07-10 10:01:22 +010050 int compr_ret;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Woodhouseef53cb02007-07-10 10:01:22 +010056 switch (jffs2_compression_mode) {
57 case JFFS2_COMPR_MODE_NONE:
58 break;
59 case JFFS2_COMPR_MODE_PRIORITY:
60 output_buf = kmalloc(*cdatalen,GFP_KERNEL);
61 if (!output_buf) {
62 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
63 goto out;
64 }
65 orig_slen = *datalen;
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))
71 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Woodhouseef53cb02007-07-10 10:01:22 +010073 this->usecount++;
74 spin_unlock(&jffs2_compressor_list_lock);
75 *datalen = orig_slen;
76 *cdatalen = orig_dlen;
77 compr_ret = this->compress(data_in, output_buf, datalen, cdatalen, NULL);
78 spin_lock(&jffs2_compressor_list_lock);
79 this->usecount--;
80 if (!compr_ret) {
81 ret = this->compr;
82 this->stat_compr_blocks++;
83 this->stat_compr_orig_size += *datalen;
84 this->stat_compr_new_size += *cdatalen;
85 break;
86 }
87 }
88 spin_unlock(&jffs2_compressor_list_lock);
89 if (ret == JFFS2_COMPR_NONE)
90 kfree(output_buf);
91 break;
92 case JFFS2_COMPR_MODE_SIZE:
93 orig_slen = *datalen;
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))
99 continue;
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;
107 }
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);
112 if (!tmp_buf) {
113 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
114 continue;
115 }
116 else {
117 this->compr_buf = tmp_buf;
118 this->compr_buf_size = orig_dlen;
119 }
120 }
121 this->usecount++;
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);
127 this->usecount--;
128 if (!compr_ret) {
129 if ((!best_dlen)||(best_dlen>*cdatalen)) {
130 best_dlen = *cdatalen;
131 best_slen = *datalen;
132 best = this;
133 }
134 }
135 }
136 if (best_dlen) {
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;
145 ret = best->compr;
146 }
147 spin_unlock(&jffs2_compressor_list_lock);
148 break;
149 default:
150 printk(KERN_ERR "JFFS2: unknow compression mode.\n");
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 out:
David Woodhouseef53cb02007-07-10 10:01:22 +0100153 if (ret == JFFS2_COMPR_NONE) {
154 *cpage_out = data_in;
155 *datalen = *cdatalen;
156 none_stat_compr_blocks++;
157 none_stat_compr_size += *datalen;
158 }
159 else {
160 *cpage_out = output_buf;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return ret;
163}
164
165int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000166 uint16_t comprtype, unsigned char *cdata_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
168{
David Woodhouseef53cb02007-07-10 10:01:22 +0100169 struct jffs2_compressor *this;
170 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
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)
175 comprtype &= 0xff;
176
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);
David Woodhouseef53cb02007-07-10 10:01:22 +0100181 none_stat_decompr_blocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 case JFFS2_COMPR_ZERO:
184 memset(data_out, 0, datalen);
185 break;
186 default:
David Woodhouseef53cb02007-07-10 10:01:22 +0100187 spin_lock(&jffs2_compressor_list_lock);
188 list_for_each_entry(this, &jffs2_compressor_list, list) {
189 if (comprtype == this->compr) {
190 this->usecount++;
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);
194 if (ret) {
195 printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
196 }
197 else {
198 this->stat_decompr_blocks++;
199 }
200 this->usecount--;
201 spin_unlock(&jffs2_compressor_list_lock);
202 return ret;
203 }
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
David Woodhouseef53cb02007-07-10 10:01:22 +0100206 spin_unlock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EIO;
208 }
209 return 0;
210}
211
212int jffs2_register_compressor(struct jffs2_compressor *comp)
213{
David Woodhouseef53cb02007-07-10 10:01:22 +0100214 struct jffs2_compressor *this;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
David Woodhouseef53cb02007-07-10 10:01:22 +0100216 if (!comp->name) {
217 printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
218 return -1;
219 }
220 comp->compr_buf_size=0;
221 comp->compr_buf=NULL;
222 comp->usecount=0;
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
David Woodhouseef53cb02007-07-10 10:01:22 +0100229 spin_lock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
David Woodhouseef53cb02007-07-10 10:01:22 +0100231 list_for_each_entry(this, &jffs2_compressor_list, list) {
232 if (this->priority < comp->priority) {
233 list_add(&comp->list, this->list.prev);
234 goto out;
235 }
236 }
237 list_add_tail(&comp->list, &jffs2_compressor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238out:
David Woodhouseef53cb02007-07-10 10:01:22 +0100239 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
240 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
241 })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David Woodhouseef53cb02007-07-10 10:01:22 +0100243 spin_unlock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
David Woodhouseef53cb02007-07-10 10:01:22 +0100245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248int jffs2_unregister_compressor(struct jffs2_compressor *comp)
249{
David Woodhouseef53cb02007-07-10 10:01:22 +0100250 D2(struct jffs2_compressor *this;)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Woodhouseef53cb02007-07-10 10:01:22 +0100252 D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
David Woodhouseef53cb02007-07-10 10:01:22 +0100254 spin_lock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Woodhouseef53cb02007-07-10 10:01:22 +0100256 if (comp->usecount) {
257 spin_unlock(&jffs2_compressor_list_lock);
258 printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
259 return -1;
260 }
261 list_del(&comp->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Woodhouseef53cb02007-07-10 10:01:22 +0100263 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
264 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
265 })
266 spin_unlock(&jffs2_compressor_list_lock);
267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
271{
David Woodhouseef53cb02007-07-10 10:01:22 +0100272 if (orig != comprbuf)
273 kfree(comprbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
David Brownell7d2beb12006-05-16 16:08:10 +0100276int __init jffs2_compressors_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278/* Registering compressors */
279#ifdef CONFIG_JFFS2_ZLIB
David Woodhouseef53cb02007-07-10 10:01:22 +0100280 jffs2_zlib_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#endif
282#ifdef CONFIG_JFFS2_RTIME
David Woodhouseef53cb02007-07-10 10:01:22 +0100283 jffs2_rtime_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
285#ifdef CONFIG_JFFS2_RUBIN
David Woodhouseef53cb02007-07-10 10:01:22 +0100286 jffs2_rubinmips_init();
287 jffs2_dynrubin_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/* Setting default compression mode */
290#ifdef CONFIG_JFFS2_CMODE_NONE
David Woodhouseef53cb02007-07-10 10:01:22 +0100291 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
292 D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#else
294#ifdef CONFIG_JFFS2_CMODE_SIZE
David Woodhouseef53cb02007-07-10 10:01:22 +0100295 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
296 D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#else
David Woodhouseef53cb02007-07-10 10:01:22 +0100298 D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif
300#endif
David Woodhouseef53cb02007-07-10 10:01:22 +0100301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
David Woodhouse3bcc86f2006-06-03 00:25:50 +0100304int jffs2_compressors_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306/* Unregistering compressors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifdef CONFIG_JFFS2_RUBIN
David Woodhouseef53cb02007-07-10 10:01:22 +0100308 jffs2_dynrubin_exit();
309 jffs2_rubinmips_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#endif
311#ifdef CONFIG_JFFS2_RTIME
David Woodhouseef53cb02007-07-10 10:01:22 +0100312 jffs2_rtime_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#endif
314#ifdef CONFIG_JFFS2_ZLIB
David Woodhouseef53cb02007-07-10 10:01:22 +0100315 jffs2_zlib_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#endif
David Woodhouseef53cb02007-07-10 10:01:22 +0100317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}