sh: Default to 4-byte alignment for SLUB objects.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jffs2 / compr.c
blob485d065de41f0061e63d4c9e0833dd41089c0e19
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Created by Arjan van de Ven <arjanv@redhat.com>
7 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
8 * University of Szeged, Hungary
10 * For licensing information, see the file 'LICENCE' in this directory.
14 #include "compr.h"
16 static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
18 /* Available compressors are on this list */
19 static LIST_HEAD(jffs2_compressor_list);
21 /* Actual compression mode */
22 static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
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;
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.
36 * Returns: Lower byte to be stored with data indicating compression type used.
37 * Zero is used to show that the data could not be compressed - the
38 * compressed version was actually larger than the original.
39 * Upper byte will be used later. (soon)
41 * If the cdata buffer isn't large enough to hold all the uncompressed data,
42 * jffs2_compress should compress as much as will fit, and should set
43 * *datalen accordingly to show the amount of data which were compressed.
45 uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
46 unsigned char *data_in, unsigned char **cpage_out,
47 uint32_t *datalen, uint32_t *cdatalen)
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;
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;
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;
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;
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;
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;
115 else {
116 this->compr_buf = tmp_buf;
117 this->compr_buf_size = orig_dlen;
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;
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;
146 spin_unlock(&jffs2_compressor_list_lock);
147 break;
148 default:
149 printk(KERN_ERR "JFFS2: unknow compression mode.\n");
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;
158 else {
159 *cpage_out = output_buf;
161 return ret;
164 int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
165 uint16_t comprtype, unsigned char *cdata_in,
166 unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
168 struct jffs2_compressor *this;
169 int ret;
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;
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);
196 else {
197 this->stat_decompr_blocks++;
199 this->usecount--;
200 spin_unlock(&jffs2_compressor_list_lock);
201 return ret;
204 printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
205 spin_unlock(&jffs2_compressor_list_lock);
206 return -EIO;
208 return 0;
211 int jffs2_register_compressor(struct jffs2_compressor *comp)
213 struct jffs2_compressor *this;
215 if (!comp->name) {
216 printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
217 return -1;
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));
228 spin_lock(&jffs2_compressor_list_lock);
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;
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);
242 spin_unlock(&jffs2_compressor_list_lock);
244 return 0;
247 int jffs2_unregister_compressor(struct jffs2_compressor *comp)
249 D2(struct jffs2_compressor *this;)
251 D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
253 spin_lock(&jffs2_compressor_list_lock);
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;
260 list_del(&comp->list);
262 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
263 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
265 spin_unlock(&jffs2_compressor_list_lock);
266 return 0;
269 void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
271 if (orig != comprbuf)
272 kfree(comprbuf);
275 int __init jffs2_compressors_init(void)
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
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;
303 int jffs2_compressors_exit(void)
305 /* Unregistering compressors */
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;