Blame view
fs/jffs2/compr.c
11.6 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 |
/* * JFFS2 -- Journalling Flash File System, Version 2. * |
c00c310ea [JFFS2] Tidy up l... |
4 |
* Copyright © 2001-2007 Red Hat, Inc. |
6088c0587 jffs2: Update cop... |
5 |
* Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> |
c00c310ea [JFFS2] Tidy up l... |
6 |
* Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>, |
ef53cb02f [JFFS2] Whitespac... |
7 |
* University of Szeged, Hungary |
1da177e4c Linux-2.6.12-rc2 |
8 |
* |
6088c0587 jffs2: Update cop... |
9 10 |
* Created by Arjan van de Ven <arjan@infradead.org> * |
1da177e4c Linux-2.6.12-rc2 |
11 12 |
* For licensing information, see the file 'LICENCE' in this directory. * |
1da177e4c Linux-2.6.12-rc2 |
13 |
*/ |
5a528957e jffs2: Use pr_fmt... |
14 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4c Linux-2.6.12-rc2 |
15 16 17 18 19 20 21 22 23 24 25 26 |
#include "compr.h" static DEFINE_SPINLOCK(jffs2_compressor_list_lock); /* Available compressors are on this list */ static LIST_HEAD(jffs2_compressor_list); /* Actual compression mode */ static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY; /* Statistics for blocks stored without compression */ static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0; |
3b23c1f5f [JFFS2] Add a "fa... |
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* * Return 1 to use this compression */ static int jffs2_is_best_compression(struct jffs2_compressor *this, struct jffs2_compressor *best, uint32_t size, uint32_t bestsize) { switch (jffs2_compression_mode) { case JFFS2_COMPR_MODE_SIZE: if (bestsize > size) return 1; return 0; case JFFS2_COMPR_MODE_FAVOURLZO: if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size)) return 1; if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size)) return 1; if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100))) return 1; if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size) return 1; return 0; } /* Shouldn't happen */ return 0; } |
123005f3c jffs2: add compr=... |
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/* * jffs2_selected_compress: * @compr: Explicit compression type to use (ie, JFFS2_COMPR_ZLIB). * If 0, just take the first available compression mode. * @data_in: Pointer to uncompressed data * @cpage_out: Pointer to returned pointer to buffer for compressed data * @datalen: On entry, holds the amount of data available for compression. * On exit, expected to hold the amount of data actually compressed. * @cdatalen: On entry, holds the amount of space available for compressed * data. On exit, expected to hold the actual size of the compressed * data. * * Returns: the compression type used. Zero is used to show that the data * could not be compressed; probably because we couldn't find the requested * compression mode. */ static int jffs2_selected_compress(u8 compr, unsigned char *data_in, unsigned char **cpage_out, u32 *datalen, u32 *cdatalen) { struct jffs2_compressor *this; int err, ret = JFFS2_COMPR_NONE; uint32_t orig_slen, orig_dlen; char *output_buf; output_buf = kmalloc(*cdatalen, GFP_KERNEL); if (!output_buf) { |
5a528957e jffs2: Use pr_fmt... |
80 81 |
pr_warn("No memory for compressor allocation. Compression failed. "); |
123005f3c jffs2: add compr=... |
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
return ret; } orig_slen = *datalen; orig_dlen = *cdatalen; spin_lock(&jffs2_compressor_list_lock); list_for_each_entry(this, &jffs2_compressor_list, list) { /* Skip decompress-only and disabled modules */ if (!this->compress || this->disabled) continue; /* Skip if not the desired compression type */ if (compr && (compr != this->compr)) continue; /* * Either compression type was unspecified, or we found our * compressor; either way, we're good to go. */ this->usecount++; spin_unlock(&jffs2_compressor_list_lock); *datalen = orig_slen; *cdatalen = orig_dlen; err = this->compress(data_in, output_buf, datalen, cdatalen); spin_lock(&jffs2_compressor_list_lock); this->usecount--; if (!err) { /* Success */ ret = this->compr; this->stat_compr_blocks++; this->stat_compr_orig_size += *datalen; this->stat_compr_new_size += *cdatalen; break; } } spin_unlock(&jffs2_compressor_list_lock); if (ret == JFFS2_COMPR_NONE) kfree(output_buf); else *cpage_out = output_buf; return ret; } |
1da177e4c Linux-2.6.12-rc2 |
126 |
/* jffs2_compress: |
faa5c2a15 [JFFS2] Correct p... |
127 128 |
* @data_in: Pointer to uncompressed data * @cpage_out: Pointer to returned pointer to buffer for compressed data |
1da177e4c Linux-2.6.12-rc2 |
129 130 131 132 133 134 135 |
* @datalen: On entry, holds the amount of data available for compression. * On exit, expected to hold the amount of data actually compressed. * @cdatalen: On entry, holds the amount of space available for compressed * data. On exit, expected to hold the actual size of the compressed * data. * * Returns: Lower byte to be stored with data indicating compression type used. |
182ec4eee [JFFS2] Clean up ... |
136 |
* Zero is used to show that the data could not be compressed - the |
1da177e4c Linux-2.6.12-rc2 |
137 138 139 140 |
* compressed version was actually larger than the original. * Upper byte will be used later. (soon) * * If the cdata buffer isn't large enough to hold all the uncompressed data, |
182ec4eee [JFFS2] Clean up ... |
141 |
* jffs2_compress should compress as much as will fit, and should set |
1da177e4c Linux-2.6.12-rc2 |
142 143 144 |
* *datalen accordingly to show the amount of data which were compressed. */ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f, |
ef53cb02f [JFFS2] Whitespac... |
145 146 |
unsigned char *data_in, unsigned char **cpage_out, uint32_t *datalen, uint32_t *cdatalen) |
1da177e4c Linux-2.6.12-rc2 |
147 148 |
{ int ret = JFFS2_COMPR_NONE; |
92abc475d jffs2: implement ... |
149 |
int mode, compr_ret; |
ef53cb02f [JFFS2] Whitespac... |
150 151 152 153 |
struct jffs2_compressor *this, *best=NULL; unsigned char *output_buf = NULL, *tmp_buf; uint32_t orig_slen, orig_dlen; uint32_t best_slen=0, best_dlen=0; |
1da177e4c Linux-2.6.12-rc2 |
154 |
|
92abc475d jffs2: implement ... |
155 156 157 158 159 160 |
if (c->mount_opts.override_compr) mode = c->mount_opts.compr; else mode = jffs2_compression_mode; switch (mode) { |
ef53cb02f [JFFS2] Whitespac... |
161 162 163 |
case JFFS2_COMPR_MODE_NONE: break; case JFFS2_COMPR_MODE_PRIORITY: |
123005f3c jffs2: add compr=... |
164 165 |
ret = jffs2_selected_compress(0, data_in, cpage_out, datalen, cdatalen); |
ef53cb02f [JFFS2] Whitespac... |
166 167 |
break; case JFFS2_COMPR_MODE_SIZE: |
3b23c1f5f [JFFS2] Add a "fa... |
168 |
case JFFS2_COMPR_MODE_FAVOURLZO: |
ef53cb02f [JFFS2] Whitespac... |
169 170 171 172 173 174 175 176 |
orig_slen = *datalen; orig_dlen = *cdatalen; spin_lock(&jffs2_compressor_list_lock); list_for_each_entry(this, &jffs2_compressor_list, list) { /* Skip decompress-only backwards-compatibility and disabled modules */ if ((!this->compress)||(this->disabled)) continue; /* Allocating memory for output buffer if necessary */ |
3b23c1f5f [JFFS2] Add a "fa... |
177 |
if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) { |
ef53cb02f [JFFS2] Whitespac... |
178 179 180 181 182 183 184 185 |
spin_unlock(&jffs2_compressor_list_lock); kfree(this->compr_buf); spin_lock(&jffs2_compressor_list_lock); this->compr_buf_size=0; this->compr_buf=NULL; } if (!this->compr_buf) { spin_unlock(&jffs2_compressor_list_lock); |
3b23c1f5f [JFFS2] Add a "fa... |
186 |
tmp_buf = kmalloc(orig_slen, GFP_KERNEL); |
ef53cb02f [JFFS2] Whitespac... |
187 188 |
spin_lock(&jffs2_compressor_list_lock); if (!tmp_buf) { |
5a528957e jffs2: Use pr_fmt... |
189 190 |
pr_warn("No memory for compressor allocation. (%d bytes) ", |
da320f055 jffs2: Convert pr... |
191 |
orig_slen); |
ef53cb02f [JFFS2] Whitespac... |
192 193 194 195 |
continue; } else { this->compr_buf = tmp_buf; |
3b23c1f5f [JFFS2] Add a "fa... |
196 |
this->compr_buf_size = orig_slen; |
ef53cb02f [JFFS2] Whitespac... |
197 198 199 200 201 202 |
} } this->usecount++; spin_unlock(&jffs2_compressor_list_lock); *datalen = orig_slen; *cdatalen = orig_dlen; |
088bd455c jffs2: drop unuse... |
203 |
compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen); |
ef53cb02f [JFFS2] Whitespac... |
204 205 206 |
spin_lock(&jffs2_compressor_list_lock); this->usecount--; if (!compr_ret) { |
3b23c1f5f [JFFS2] Add a "fa... |
207 208 |
if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen)) && (*cdatalen < *datalen)) { |
ef53cb02f [JFFS2] Whitespac... |
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
best_dlen = *cdatalen; best_slen = *datalen; best = this; } } } if (best_dlen) { *cdatalen = best_dlen; *datalen = best_slen; output_buf = best->compr_buf; best->compr_buf = NULL; best->compr_buf_size = 0; best->stat_compr_blocks++; best->stat_compr_orig_size += best_slen; best->stat_compr_new_size += best_dlen; ret = best->compr; |
123005f3c jffs2: add compr=... |
225 |
*cpage_out = output_buf; |
ef53cb02f [JFFS2] Whitespac... |
226 227 228 |
} spin_unlock(&jffs2_compressor_list_lock); break; |
123005f3c jffs2: add compr=... |
229 230 231 232 233 234 235 236 |
case JFFS2_COMPR_MODE_FORCELZO: ret = jffs2_selected_compress(JFFS2_COMPR_LZO, data_in, cpage_out, datalen, cdatalen); break; case JFFS2_COMPR_MODE_FORCEZLIB: ret = jffs2_selected_compress(JFFS2_COMPR_ZLIB, data_in, cpage_out, datalen, cdatalen); break; |
ef53cb02f [JFFS2] Whitespac... |
237 |
default: |
5a528957e jffs2: Use pr_fmt... |
238 239 |
pr_err("unknown compression mode "); |
ef53cb02f [JFFS2] Whitespac... |
240 |
} |
123005f3c jffs2: add compr=... |
241 |
|
ef53cb02f [JFFS2] Whitespac... |
242 243 244 245 246 247 |
if (ret == JFFS2_COMPR_NONE) { *cpage_out = data_in; *datalen = *cdatalen; none_stat_compr_blocks++; none_stat_compr_size += *datalen; } |
1da177e4c Linux-2.6.12-rc2 |
248 249 250 251 |
return ret; } int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f, |
182ec4eee [JFFS2] Clean up ... |
252 |
uint16_t comprtype, unsigned char *cdata_in, |
1da177e4c Linux-2.6.12-rc2 |
253 254 |
unsigned char *data_out, uint32_t cdatalen, uint32_t datalen) { |
ef53cb02f [JFFS2] Whitespac... |
255 256 |
struct jffs2_compressor *this; int ret; |
1da177e4c Linux-2.6.12-rc2 |
257 258 259 260 261 262 263 264 265 266 |
/* Older code had a bug where it would write non-zero 'usercompr' fields. Deal with it. */ if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB) comprtype &= 0xff; switch (comprtype & 0xff) { case JFFS2_COMPR_NONE: /* This should be special-cased elsewhere, but we might as well deal with it */ memcpy(data_out, cdata_in, datalen); |
ef53cb02f [JFFS2] Whitespac... |
267 |
none_stat_decompr_blocks++; |
1da177e4c Linux-2.6.12-rc2 |
268 269 270 271 272 |
break; case JFFS2_COMPR_ZERO: memset(data_out, 0, datalen); break; default: |
ef53cb02f [JFFS2] Whitespac... |
273 274 275 276 277 |
spin_lock(&jffs2_compressor_list_lock); list_for_each_entry(this, &jffs2_compressor_list, list) { if (comprtype == this->compr) { this->usecount++; spin_unlock(&jffs2_compressor_list_lock); |
088bd455c jffs2: drop unuse... |
278 |
ret = this->decompress(cdata_in, data_out, cdatalen, datalen); |
ef53cb02f [JFFS2] Whitespac... |
279 280 |
spin_lock(&jffs2_compressor_list_lock); if (ret) { |
da320f055 jffs2: Convert pr... |
281 282 283 |
pr_warn("Decompressor \"%s\" returned %d ", this->name, ret); |
ef53cb02f [JFFS2] Whitespac... |
284 285 286 287 288 289 290 291 292 |
} else { this->stat_decompr_blocks++; } this->usecount--; spin_unlock(&jffs2_compressor_list_lock); return ret; } } |
5a528957e jffs2: Use pr_fmt... |
293 294 |
pr_warn("compression type 0x%02x not available ", comprtype); |
ef53cb02f [JFFS2] Whitespac... |
295 |
spin_unlock(&jffs2_compressor_list_lock); |
1da177e4c Linux-2.6.12-rc2 |
296 297 298 299 300 301 302 |
return -EIO; } return 0; } int jffs2_register_compressor(struct jffs2_compressor *comp) { |
ef53cb02f [JFFS2] Whitespac... |
303 |
struct jffs2_compressor *this; |
1da177e4c Linux-2.6.12-rc2 |
304 |
|
ef53cb02f [JFFS2] Whitespac... |
305 |
if (!comp->name) { |
da320f055 jffs2: Convert pr... |
306 307 |
pr_warn("NULL compressor name at registering JFFS2 compressor. Failed. "); |
ef53cb02f [JFFS2] Whitespac... |
308 309 310 311 312 313 314 315 316 |
return -1; } comp->compr_buf_size=0; comp->compr_buf=NULL; comp->usecount=0; comp->stat_compr_orig_size=0; comp->stat_compr_new_size=0; comp->stat_compr_blocks=0; comp->stat_decompr_blocks=0; |
9c261b33a jffs2: Convert mo... |
317 318 |
jffs2_dbg(1, "Registering JFFS2 compressor \"%s\" ", comp->name); |
1da177e4c Linux-2.6.12-rc2 |
319 |
|
ef53cb02f [JFFS2] Whitespac... |
320 |
spin_lock(&jffs2_compressor_list_lock); |
1da177e4c Linux-2.6.12-rc2 |
321 |
|
ef53cb02f [JFFS2] Whitespac... |
322 323 324 325 326 327 328 |
list_for_each_entry(this, &jffs2_compressor_list, list) { if (this->priority < comp->priority) { list_add(&comp->list, this->list.prev); goto out; } } list_add_tail(&comp->list, &jffs2_compressor_list); |
1da177e4c Linux-2.6.12-rc2 |
329 |
out: |
ef53cb02f [JFFS2] Whitespac... |
330 331 332 333 |
D2(list_for_each_entry(this, &jffs2_compressor_list, list) { printk(KERN_DEBUG "Compressor \"%s\", prio %d ", this->name, this->priority); }) |
1da177e4c Linux-2.6.12-rc2 |
334 |
|
ef53cb02f [JFFS2] Whitespac... |
335 |
spin_unlock(&jffs2_compressor_list_lock); |
1da177e4c Linux-2.6.12-rc2 |
336 |
|
ef53cb02f [JFFS2] Whitespac... |
337 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
338 339 340 341 |
} int jffs2_unregister_compressor(struct jffs2_compressor *comp) { |
9c261b33a jffs2: Convert mo... |
342 |
D2(struct jffs2_compressor *this); |
1da177e4c Linux-2.6.12-rc2 |
343 |
|
9c261b33a jffs2: Convert mo... |
344 345 |
jffs2_dbg(1, "Unregistering JFFS2 compressor \"%s\" ", comp->name); |
1da177e4c Linux-2.6.12-rc2 |
346 |
|
ef53cb02f [JFFS2] Whitespac... |
347 |
spin_lock(&jffs2_compressor_list_lock); |
1da177e4c Linux-2.6.12-rc2 |
348 |
|
ef53cb02f [JFFS2] Whitespac... |
349 350 |
if (comp->usecount) { spin_unlock(&jffs2_compressor_list_lock); |
5a528957e jffs2: Use pr_fmt... |
351 352 |
pr_warn("Compressor module is in use. Unregister failed. "); |
ef53cb02f [JFFS2] Whitespac... |
353 354 355 |
return -1; } list_del(&comp->list); |
1da177e4c Linux-2.6.12-rc2 |
356 |
|
ef53cb02f [JFFS2] Whitespac... |
357 358 359 360 361 362 |
D2(list_for_each_entry(this, &jffs2_compressor_list, list) { printk(KERN_DEBUG "Compressor \"%s\", prio %d ", this->name, this->priority); }) spin_unlock(&jffs2_compressor_list_lock); return 0; |
1da177e4c Linux-2.6.12-rc2 |
363 |
} |
1da177e4c Linux-2.6.12-rc2 |
364 365 |
void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig) { |
ef53cb02f [JFFS2] Whitespac... |
366 367 |
if (orig != comprbuf) kfree(comprbuf); |
1da177e4c Linux-2.6.12-rc2 |
368 |
} |
7d2beb135 [JFFS2] Fix secti... |
369 |
int __init jffs2_compressors_init(void) |
1da177e4c Linux-2.6.12-rc2 |
370 371 372 |
{ /* Registering compressors */ #ifdef CONFIG_JFFS2_ZLIB |
ef53cb02f [JFFS2] Whitespac... |
373 |
jffs2_zlib_init(); |
1da177e4c Linux-2.6.12-rc2 |
374 375 |
#endif #ifdef CONFIG_JFFS2_RTIME |
ef53cb02f [JFFS2] Whitespac... |
376 |
jffs2_rtime_init(); |
1da177e4c Linux-2.6.12-rc2 |
377 378 |
#endif #ifdef CONFIG_JFFS2_RUBIN |
ef53cb02f [JFFS2] Whitespac... |
379 380 |
jffs2_rubinmips_init(); jffs2_dynrubin_init(); |
1da177e4c Linux-2.6.12-rc2 |
381 |
#endif |
c799aca31 [JFFS2] Add LZO c... |
382 383 384 |
#ifdef CONFIG_JFFS2_LZO jffs2_lzo_init(); #endif |
1da177e4c Linux-2.6.12-rc2 |
385 386 |
/* Setting default compression mode */ #ifdef CONFIG_JFFS2_CMODE_NONE |
ef53cb02f [JFFS2] Whitespac... |
387 |
jffs2_compression_mode = JFFS2_COMPR_MODE_NONE; |
5a528957e jffs2: Use pr_fmt... |
388 389 |
jffs2_dbg(1, "default compression mode: none "); |
1da177e4c Linux-2.6.12-rc2 |
390 391 |
#else #ifdef CONFIG_JFFS2_CMODE_SIZE |
ef53cb02f [JFFS2] Whitespac... |
392 |
jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE; |
5a528957e jffs2: Use pr_fmt... |
393 394 |
jffs2_dbg(1, "default compression mode: size "); |
1da177e4c Linux-2.6.12-rc2 |
395 |
#else |
3b23c1f5f [JFFS2] Add a "fa... |
396 397 |
#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO; |
5a528957e jffs2: Use pr_fmt... |
398 399 |
jffs2_dbg(1, "default compression mode: favourlzo "); |
3b23c1f5f [JFFS2] Add a "fa... |
400 |
#else |
5a528957e jffs2: Use pr_fmt... |
401 402 |
jffs2_dbg(1, "default compression mode: priority "); |
1da177e4c Linux-2.6.12-rc2 |
403 404 |
#endif #endif |
3b23c1f5f [JFFS2] Add a "fa... |
405 |
#endif |
ef53cb02f [JFFS2] Whitespac... |
406 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
407 |
} |
3bcc86f50 [JFFS2] Remove st... |
408 |
int jffs2_compressors_exit(void) |
1da177e4c Linux-2.6.12-rc2 |
409 410 |
{ /* Unregistering compressors */ |
c799aca31 [JFFS2] Add LZO c... |
411 412 413 |
#ifdef CONFIG_JFFS2_LZO jffs2_lzo_exit(); #endif |
1da177e4c Linux-2.6.12-rc2 |
414 |
#ifdef CONFIG_JFFS2_RUBIN |
ef53cb02f [JFFS2] Whitespac... |
415 416 |
jffs2_dynrubin_exit(); jffs2_rubinmips_exit(); |
1da177e4c Linux-2.6.12-rc2 |
417 418 |
#endif #ifdef CONFIG_JFFS2_RTIME |
ef53cb02f [JFFS2] Whitespac... |
419 |
jffs2_rtime_exit(); |
1da177e4c Linux-2.6.12-rc2 |
420 421 |
#endif #ifdef CONFIG_JFFS2_ZLIB |
ef53cb02f [JFFS2] Whitespac... |
422 |
jffs2_zlib_exit(); |
1da177e4c Linux-2.6.12-rc2 |
423 |
#endif |
ef53cb02f [JFFS2] Whitespac... |
424 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
425 |
} |