Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
3 | * | |
4 | * Copyright (C) 2001-2003 Red Hat, Inc. | |
5 | * Created by Arjan van de Ven <arjanv@redhat.com> | |
6 | * | |
7 | * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>, | |
8 | * University of Szeged, Hungary | |
9 | * | |
10 | * For licensing information, see the file 'LICENCE' in this directory. | |
11 | * | |
182ec4ee | 12 | * $Id: compr.c,v 1.46 2005/11/07 11:14:38 gleixner Exp $ |
1da177e4 LT |
13 | * |
14 | */ | |
15 | ||
16 | #include "compr.h" | |
17 | ||
18 | static DEFINE_SPINLOCK(jffs2_compressor_list_lock); | |
19 | ||
20 | /* Available compressors are on this list */ | |
21 | static LIST_HEAD(jffs2_compressor_list); | |
22 | ||
23 | /* Actual compression mode */ | |
24 | static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY; | |
25 | ||
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; | |
28 | ||
29 | /* jffs2_compress: | |
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 | |
36 | * data. | |
37 | * | |
38 | * Returns: Lower byte to be stored with data indicating compression type used. | |
182ec4ee | 39 | * Zero is used to show that the data could not be compressed - the |
1da177e4 LT |
40 | * compressed version was actually larger than the original. |
41 | * Upper byte will be used later. (soon) | |
42 | * | |
43 | * If the cdata buffer isn't large enough to hold all the uncompressed data, | |
182ec4ee | 44 | * jffs2_compress should compress as much as will fit, and should set |
1da177e4 LT |
45 | * *datalen accordingly to show the amount of data which were compressed. |
46 | */ | |
47 | uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f, | |
182ec4ee | 48 | unsigned char *data_in, unsigned char **cpage_out, |
1da177e4 LT |
49 | uint32_t *datalen, uint32_t *cdatalen) |
50 | { | |
51 | int ret = JFFS2_COMPR_NONE; | |
52 | int compr_ret; | |
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; | |
57 | ||
58 | switch (jffs2_compression_mode) { | |
59 | case JFFS2_COMPR_MODE_NONE: | |
60 | break; | |
61 | case JFFS2_COMPR_MODE_PRIORITY: | |
62 | output_buf = kmalloc(*cdatalen,GFP_KERNEL); | |
63 | if (!output_buf) { | |
64 | printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n"); | |
65 | goto out; | |
66 | } | |
67 | orig_slen = *datalen; | |
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)) | |
73 | continue; | |
74 | ||
75 | this->usecount++; | |
76 | spin_unlock(&jffs2_compressor_list_lock); | |
77 | *datalen = orig_slen; | |
78 | *cdatalen = orig_dlen; | |
79 | compr_ret = this->compress(data_in, output_buf, datalen, cdatalen, NULL); | |
80 | spin_lock(&jffs2_compressor_list_lock); | |
81 | this->usecount--; | |
82 | if (!compr_ret) { | |
83 | ret = this->compr; | |
84 | this->stat_compr_blocks++; | |
85 | this->stat_compr_orig_size += *datalen; | |
86 | this->stat_compr_new_size += *cdatalen; | |
87 | break; | |
88 | } | |
89 | } | |
90 | spin_unlock(&jffs2_compressor_list_lock); | |
91 | if (ret == JFFS2_COMPR_NONE) kfree(output_buf); | |
92 | break; | |
93 | case JFFS2_COMPR_MODE_SIZE: | |
94 | orig_slen = *datalen; | |
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)) | |
100 | continue; | |
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; | |
108 | } | |
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); | |
113 | if (!tmp_buf) { | |
114 | printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen); | |
115 | continue; | |
116 | } | |
117 | else { | |
118 | this->compr_buf = tmp_buf; | |
119 | this->compr_buf_size = orig_dlen; | |
120 | } | |
121 | } | |
122 | this->usecount++; | |
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); | |
128 | this->usecount--; | |
129 | if (!compr_ret) { | |
130 | if ((!best_dlen)||(best_dlen>*cdatalen)) { | |
131 | best_dlen = *cdatalen; | |
132 | best_slen = *datalen; | |
133 | best = this; | |
134 | } | |
135 | } | |
136 | } | |
137 | if (best_dlen) { | |
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; | |
146 | ret = best->compr; | |
147 | } | |
148 | spin_unlock(&jffs2_compressor_list_lock); | |
149 | break; | |
150 | default: | |
151 | printk(KERN_ERR "JFFS2: unknow compression mode.\n"); | |
152 | } | |
153 | out: | |
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; | |
159 | } | |
160 | else { | |
161 | *cpage_out = output_buf; | |
162 | } | |
163 | return ret; | |
164 | } | |
165 | ||
166 | int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f, | |
182ec4ee | 167 | uint16_t comprtype, unsigned char *cdata_in, |
1da177e4 LT |
168 | unsigned char *data_out, uint32_t cdatalen, uint32_t datalen) |
169 | { | |
170 | struct jffs2_compressor *this; | |
171 | int ret; | |
172 | ||
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) | |
176 | comprtype &= 0xff; | |
177 | ||
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++; | |
183 | break; | |
184 | case JFFS2_COMPR_ZERO: | |
185 | memset(data_out, 0, datalen); | |
186 | break; | |
187 | default: | |
188 | spin_lock(&jffs2_compressor_list_lock); | |
189 | list_for_each_entry(this, &jffs2_compressor_list, list) { | |
190 | if (comprtype == this->compr) { | |
191 | this->usecount++; | |
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); | |
195 | if (ret) { | |
196 | printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret); | |
197 | } | |
198 | else { | |
199 | this->stat_decompr_blocks++; | |
200 | } | |
201 | this->usecount--; | |
202 | spin_unlock(&jffs2_compressor_list_lock); | |
203 | return ret; | |
204 | } | |
205 | } | |
206 | printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype); | |
207 | spin_unlock(&jffs2_compressor_list_lock); | |
208 | return -EIO; | |
209 | } | |
210 | return 0; | |
211 | } | |
212 | ||
213 | int jffs2_register_compressor(struct jffs2_compressor *comp) | |
214 | { | |
215 | struct jffs2_compressor *this; | |
216 | ||
217 | if (!comp->name) { | |
218 | printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n"); | |
219 | return -1; | |
220 | } | |
221 | comp->compr_buf_size=0; | |
222 | comp->compr_buf=NULL; | |
223 | comp->usecount=0; | |
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)); | |
229 | ||
230 | spin_lock(&jffs2_compressor_list_lock); | |
231 | ||
232 | list_for_each_entry(this, &jffs2_compressor_list, list) { | |
233 | if (this->priority < comp->priority) { | |
234 | list_add(&comp->list, this->list.prev); | |
235 | goto out; | |
236 | } | |
237 | } | |
238 | list_add_tail(&comp->list, &jffs2_compressor_list); | |
239 | out: | |
240 | D2(list_for_each_entry(this, &jffs2_compressor_list, list) { | |
241 | printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority); | |
242 | }) | |
243 | ||
244 | spin_unlock(&jffs2_compressor_list_lock); | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | int jffs2_unregister_compressor(struct jffs2_compressor *comp) | |
250 | { | |
251 | D2(struct jffs2_compressor *this;) | |
252 | ||
253 | D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name)); | |
254 | ||
255 | spin_lock(&jffs2_compressor_list_lock); | |
256 | ||
257 | if (comp->usecount) { | |
258 | spin_unlock(&jffs2_compressor_list_lock); | |
259 | printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n"); | |
260 | return -1; | |
261 | } | |
262 | list_del(&comp->list); | |
263 | ||
264 | D2(list_for_each_entry(this, &jffs2_compressor_list, list) { | |
265 | printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority); | |
266 | }) | |
267 | spin_unlock(&jffs2_compressor_list_lock); | |
268 | return 0; | |
269 | } | |
270 | ||
1da177e4 LT |
271 | void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig) |
272 | { | |
273 | if (orig != comprbuf) | |
274 | kfree(comprbuf); | |
275 | } | |
276 | ||
7d2beb13 | 277 | int __init jffs2_compressors_init(void) |
1da177e4 LT |
278 | { |
279 | /* Registering compressors */ | |
280 | #ifdef CONFIG_JFFS2_ZLIB | |
281 | jffs2_zlib_init(); | |
282 | #endif | |
283 | #ifdef CONFIG_JFFS2_RTIME | |
284 | jffs2_rtime_init(); | |
285 | #endif | |
286 | #ifdef CONFIG_JFFS2_RUBIN | |
287 | jffs2_rubinmips_init(); | |
288 | jffs2_dynrubin_init(); | |
289 | #endif | |
1da177e4 LT |
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");) | |
294 | #else | |
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");) | |
298 | #else | |
299 | D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");) | |
300 | #endif | |
301 | #endif | |
302 | return 0; | |
303 | } | |
304 | ||
3bcc86f5 | 305 | int jffs2_compressors_exit(void) |
1da177e4 LT |
306 | { |
307 | /* Unregistering compressors */ | |
1da177e4 LT |
308 | #ifdef CONFIG_JFFS2_RUBIN |
309 | jffs2_dynrubin_exit(); | |
310 | jffs2_rubinmips_exit(); | |
311 | #endif | |
312 | #ifdef CONFIG_JFFS2_RTIME | |
313 | jffs2_rtime_exit(); | |
314 | #endif | |
315 | #ifdef CONFIG_JFFS2_ZLIB | |
316 | jffs2_zlib_exit(); | |
317 | #endif | |
318 | return 0; | |
319 | } |