Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
David Woodhouse | c00c310 | 2007-04-25 14:16:47 +0100 | [diff] [blame] | 4 | * Copyright © 2001-2007 Red Hat, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Created by Arjan van de Ven <arjanv@redhat.com> |
| 6 | * |
David Woodhouse | c00c310 | 2007-04-25 14:16:47 +0100 | [diff] [blame] | 7 | * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * University of Szeged, Hungary |
| 9 | * |
| 10 | * For licensing information, see the file 'LICENCE' in this directory. |
| 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "compr.h" |
| 15 | |
| 16 | static DEFINE_SPINLOCK(jffs2_compressor_list_lock); |
| 17 | |
| 18 | /* Available compressors are on this list */ |
| 19 | static LIST_HEAD(jffs2_compressor_list); |
| 20 | |
| 21 | /* Actual compression mode */ |
| 22 | static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY; |
| 23 | |
| 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; |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 37 | * Zero is used to show that the data could not be compressed - the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 42 | * jffs2_compress should compress as much as will fit, and should set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * *datalen accordingly to show the amount of data which were compressed. |
| 44 | */ |
| 45 | uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f, |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 46 | unsigned char *data_in, unsigned char **cpage_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | uint32_t *datalen, uint32_t *cdatalen) |
| 48 | { |
| 49 | int ret = JFFS2_COMPR_NONE; |
| 50 | 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; |
| 55 | |
| 56 | 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; |
| 72 | |
| 73 | 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) kfree(output_buf); |
| 90 | break; |
| 91 | case JFFS2_COMPR_MODE_SIZE: |
| 92 | orig_slen = *datalen; |
| 93 | orig_dlen = *cdatalen; |
| 94 | spin_lock(&jffs2_compressor_list_lock); |
| 95 | list_for_each_entry(this, &jffs2_compressor_list, list) { |
| 96 | /* Skip decompress-only backwards-compatibility and disabled modules */ |
| 97 | if ((!this->compress)||(this->disabled)) |
| 98 | continue; |
| 99 | /* Allocating memory for output buffer if necessary */ |
| 100 | if ((this->compr_buf_size<orig_dlen)&&(this->compr_buf)) { |
| 101 | spin_unlock(&jffs2_compressor_list_lock); |
| 102 | kfree(this->compr_buf); |
| 103 | spin_lock(&jffs2_compressor_list_lock); |
| 104 | this->compr_buf_size=0; |
| 105 | this->compr_buf=NULL; |
| 106 | } |
| 107 | if (!this->compr_buf) { |
| 108 | spin_unlock(&jffs2_compressor_list_lock); |
| 109 | tmp_buf = kmalloc(orig_dlen,GFP_KERNEL); |
| 110 | spin_lock(&jffs2_compressor_list_lock); |
| 111 | if (!tmp_buf) { |
| 112 | printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen); |
| 113 | continue; |
| 114 | } |
| 115 | else { |
| 116 | this->compr_buf = tmp_buf; |
| 117 | this->compr_buf_size = orig_dlen; |
| 118 | } |
| 119 | } |
| 120 | this->usecount++; |
| 121 | spin_unlock(&jffs2_compressor_list_lock); |
| 122 | *datalen = orig_slen; |
| 123 | *cdatalen = orig_dlen; |
| 124 | compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen, NULL); |
| 125 | spin_lock(&jffs2_compressor_list_lock); |
| 126 | this->usecount--; |
| 127 | if (!compr_ret) { |
| 128 | if ((!best_dlen)||(best_dlen>*cdatalen)) { |
| 129 | best_dlen = *cdatalen; |
| 130 | best_slen = *datalen; |
| 131 | best = this; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | if (best_dlen) { |
| 136 | *cdatalen = best_dlen; |
| 137 | *datalen = best_slen; |
| 138 | output_buf = best->compr_buf; |
| 139 | best->compr_buf = NULL; |
| 140 | best->compr_buf_size = 0; |
| 141 | best->stat_compr_blocks++; |
| 142 | best->stat_compr_orig_size += best_slen; |
| 143 | best->stat_compr_new_size += best_dlen; |
| 144 | ret = best->compr; |
| 145 | } |
| 146 | spin_unlock(&jffs2_compressor_list_lock); |
| 147 | break; |
| 148 | default: |
| 149 | printk(KERN_ERR "JFFS2: unknow compression mode.\n"); |
| 150 | } |
| 151 | out: |
| 152 | if (ret == JFFS2_COMPR_NONE) { |
| 153 | *cpage_out = data_in; |
| 154 | *datalen = *cdatalen; |
| 155 | none_stat_compr_blocks++; |
| 156 | none_stat_compr_size += *datalen; |
| 157 | } |
| 158 | else { |
| 159 | *cpage_out = output_buf; |
| 160 | } |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f, |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 165 | uint16_t comprtype, unsigned char *cdata_in, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | unsigned char *data_out, uint32_t cdatalen, uint32_t datalen) |
| 167 | { |
| 168 | struct jffs2_compressor *this; |
| 169 | int ret; |
| 170 | |
| 171 | /* Older code had a bug where it would write non-zero 'usercompr' |
| 172 | fields. Deal with it. */ |
| 173 | if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB) |
| 174 | comprtype &= 0xff; |
| 175 | |
| 176 | switch (comprtype & 0xff) { |
| 177 | case JFFS2_COMPR_NONE: |
| 178 | /* This should be special-cased elsewhere, but we might as well deal with it */ |
| 179 | memcpy(data_out, cdata_in, datalen); |
| 180 | none_stat_decompr_blocks++; |
| 181 | break; |
| 182 | case JFFS2_COMPR_ZERO: |
| 183 | memset(data_out, 0, datalen); |
| 184 | break; |
| 185 | default: |
| 186 | spin_lock(&jffs2_compressor_list_lock); |
| 187 | list_for_each_entry(this, &jffs2_compressor_list, list) { |
| 188 | if (comprtype == this->compr) { |
| 189 | this->usecount++; |
| 190 | spin_unlock(&jffs2_compressor_list_lock); |
| 191 | ret = this->decompress(cdata_in, data_out, cdatalen, datalen, NULL); |
| 192 | spin_lock(&jffs2_compressor_list_lock); |
| 193 | if (ret) { |
| 194 | printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret); |
| 195 | } |
| 196 | else { |
| 197 | this->stat_decompr_blocks++; |
| 198 | } |
| 199 | this->usecount--; |
| 200 | spin_unlock(&jffs2_compressor_list_lock); |
| 201 | return ret; |
| 202 | } |
| 203 | } |
| 204 | printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype); |
| 205 | spin_unlock(&jffs2_compressor_list_lock); |
| 206 | return -EIO; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int jffs2_register_compressor(struct jffs2_compressor *comp) |
| 212 | { |
| 213 | struct jffs2_compressor *this; |
| 214 | |
| 215 | if (!comp->name) { |
| 216 | printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n"); |
| 217 | return -1; |
| 218 | } |
| 219 | comp->compr_buf_size=0; |
| 220 | comp->compr_buf=NULL; |
| 221 | comp->usecount=0; |
| 222 | comp->stat_compr_orig_size=0; |
| 223 | comp->stat_compr_new_size=0; |
| 224 | comp->stat_compr_blocks=0; |
| 225 | comp->stat_decompr_blocks=0; |
| 226 | D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name)); |
| 227 | |
| 228 | spin_lock(&jffs2_compressor_list_lock); |
| 229 | |
| 230 | list_for_each_entry(this, &jffs2_compressor_list, list) { |
| 231 | if (this->priority < comp->priority) { |
| 232 | list_add(&comp->list, this->list.prev); |
| 233 | goto out; |
| 234 | } |
| 235 | } |
| 236 | list_add_tail(&comp->list, &jffs2_compressor_list); |
| 237 | out: |
| 238 | D2(list_for_each_entry(this, &jffs2_compressor_list, list) { |
| 239 | printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority); |
| 240 | }) |
| 241 | |
| 242 | spin_unlock(&jffs2_compressor_list_lock); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int jffs2_unregister_compressor(struct jffs2_compressor *comp) |
| 248 | { |
| 249 | D2(struct jffs2_compressor *this;) |
| 250 | |
| 251 | D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name)); |
| 252 | |
| 253 | spin_lock(&jffs2_compressor_list_lock); |
| 254 | |
| 255 | if (comp->usecount) { |
| 256 | spin_unlock(&jffs2_compressor_list_lock); |
| 257 | printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n"); |
| 258 | return -1; |
| 259 | } |
| 260 | list_del(&comp->list); |
| 261 | |
| 262 | D2(list_for_each_entry(this, &jffs2_compressor_list, list) { |
| 263 | printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority); |
| 264 | }) |
| 265 | spin_unlock(&jffs2_compressor_list_lock); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig) |
| 270 | { |
| 271 | if (orig != comprbuf) |
| 272 | kfree(comprbuf); |
| 273 | } |
| 274 | |
David Brownell | 7d2beb1 | 2006-05-16 16:08:10 +0100 | [diff] [blame] | 275 | int __init jffs2_compressors_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | /* Registering compressors */ |
| 278 | #ifdef CONFIG_JFFS2_ZLIB |
| 279 | jffs2_zlib_init(); |
| 280 | #endif |
| 281 | #ifdef CONFIG_JFFS2_RTIME |
| 282 | jffs2_rtime_init(); |
| 283 | #endif |
| 284 | #ifdef CONFIG_JFFS2_RUBIN |
| 285 | jffs2_rubinmips_init(); |
| 286 | jffs2_dynrubin_init(); |
| 287 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | /* Setting default compression mode */ |
| 289 | #ifdef CONFIG_JFFS2_CMODE_NONE |
| 290 | jffs2_compression_mode = JFFS2_COMPR_MODE_NONE; |
| 291 | D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");) |
| 292 | #else |
| 293 | #ifdef CONFIG_JFFS2_CMODE_SIZE |
| 294 | jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE; |
| 295 | D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");) |
| 296 | #else |
| 297 | D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");) |
| 298 | #endif |
| 299 | #endif |
| 300 | return 0; |
| 301 | } |
| 302 | |
David Woodhouse | 3bcc86f | 2006-06-03 00:25:50 +0100 | [diff] [blame] | 303 | int jffs2_compressors_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | /* Unregistering compressors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | #ifdef CONFIG_JFFS2_RUBIN |
| 307 | jffs2_dynrubin_exit(); |
| 308 | jffs2_rubinmips_exit(); |
| 309 | #endif |
| 310 | #ifdef CONFIG_JFFS2_RTIME |
| 311 | jffs2_rtime_exit(); |
| 312 | #endif |
| 313 | #ifdef CONFIG_JFFS2_ZLIB |
| 314 | jffs2_zlib_exit(); |
| 315 | #endif |
| 316 | return 0; |
| 317 | } |