1 diff -Nru a/fs/squashfs/decompressor.c b/fs/squashfs/decompressor.c
2 --- a/fs/squashfs/decompressor.c 2010-05-16 23:17:36.000000000 +0200
3 +++ b/fs/squashfs/decompressor.c 2010-05-17 14:57:45.271547099 +0200
6 static const struct squashfs_decompressor *decompressor[] = {
7 &squashfs_zlib_comp_ops,
8 +#ifdef CONFIG_SQUASHFS_LZMA
9 + &squashfs_lzma_comp_ops,
11 &squashfs_lzma_unsupported_comp_ops,
13 &squashfs_lzo_unsupported_comp_ops,
14 &squashfs_unknown_comp_ops
16 diff -Nru a/fs/squashfs/Kconfig b/fs/squashfs/Kconfig
17 --- a/fs/squashfs/Kconfig 2010-05-16 23:17:36.000000000 +0200
18 +++ b/fs/squashfs/Kconfig 2010-05-17 15:13:49.807545765 +0200
24 + bool "Include support for LZMA compressed file systems"
26 + select DECOMPRESS_LZMA
27 + select DECOMPRESS_LZMA_NEEDED
29 config SQUASHFS_EMBEDDED
31 bool "Additional option for memory-constrained systems"
32 diff -Nru a/fs/squashfs/lzma_wrapper.c b/fs/squashfs/lzma_wrapper.c
33 --- a/fs/squashfs/lzma_wrapper.c 1970-01-01 01:00:00.000000000 +0100
34 +++ b/fs/squashfs/lzma_wrapper.c 2010-05-17 15:15:12.637552661 +0200
37 + * Squashfs - a compressed read only filesystem for Linux
39 + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
40 + * Phillip Lougher <phillip@lougher.demon.co.uk>
42 + * This program is free software; you can redistribute it and/or
43 + * modify it under the terms of the GNU General Public License
44 + * as published by the Free Software Foundation; either version 2,
45 + * or (at your option) any later version.
47 + * This program is distributed in the hope that it will be useful,
48 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 + * GNU General Public License for more details.
52 + * You should have received a copy of the GNU General Public License
53 + * along with this program; if not, write to the Free Software
54 + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
59 +#include <asm/unaligned.h>
60 +#include <linux/slab.h>
61 +#include <linux/buffer_head.h>
62 +#include <linux/mutex.h>
63 +#include <linux/vmalloc.h>
64 +#include <linux/decompress/unlzma.h>
66 +#include "squashfs_fs.h"
67 +#include "squashfs_fs_sb.h"
68 +#include "squashfs_fs_i.h"
69 +#include "squashfs.h"
70 +#include "decompressor.h"
72 +struct squashfs_lzma {
77 +/* decompress_unlzma.c is currently non re-entrant... */
78 +DEFINE_MUTEX(lzma_mutex);
80 +/* decompress_unlzma.c doesn't provide any context in its callbacks... */
81 +static int lzma_error;
83 +static void error(char *m)
85 + ERROR("unlzma error: %s\n", m);
90 +static void *lzma_init(struct squashfs_sb_info *msblk)
92 + struct squashfs_lzma *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
95 + stream->input = vmalloc(msblk->block_size);
96 + if (stream->input == NULL)
98 + stream->output = vmalloc(msblk->block_size);
99 + if (stream->output == NULL)
105 + vfree(stream->input);
107 + ERROR("failed to allocate lzma workspace\n");
113 +static void lzma_free(void *strm)
115 + struct squashfs_lzma *stream = strm;
118 + vfree(stream->input);
119 + vfree(stream->output);
125 +static int lzma_uncompress(struct squashfs_sb_info *msblk, void **buffer,
126 + struct buffer_head **bh, int b, int offset, int length, int srclength,
129 + struct squashfs_lzma *stream = msblk->stream;
130 + void *buff = stream->input;
131 + int avail, i, bytes = length, res;
133 + mutex_lock(&lzma_mutex);
135 + for (i = 0; i < b; i++) {
136 + wait_on_buffer(bh[i]);
137 + if (!buffer_uptodate(bh[i]))
138 + goto block_release;
140 + avail = min(bytes, msblk->devblksize - offset);
141 + memcpy(buff, bh[i]->b_data + offset, avail);
149 + res = unlzma(stream->input, length, NULL, NULL, stream->output, NULL,
151 + if (res || lzma_error)
154 + /* uncompressed size is stored in the LZMA header (5 byte offset) */
155 + res = bytes = get_unaligned_le32(stream->input + 5);
156 + for (i = 0, buff = stream->output; bytes && i < pages; i++) {
157 + avail = min_t(int, bytes, PAGE_CACHE_SIZE);
158 + memcpy(buffer[i], buff, avail);
165 + mutex_unlock(&lzma_mutex);
173 + mutex_unlock(&lzma_mutex);
175 + ERROR("lzma decompression failed, data probably corrupt\n");
179 +const struct squashfs_decompressor squashfs_lzma_comp_ops = {
182 + .decompress = lzma_uncompress,
183 + .id = LZMA_COMPRESSION,
188 diff -Nru a/fs/squashfs/Makefile b/fs/squashfs/Makefile
189 --- a/fs/squashfs/Makefile 2010-05-16 23:17:36.000000000 +0200
190 +++ b/fs/squashfs/Makefile 2010-05-17 14:57:45.270554026 +0200
192 squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o
193 squashfs-y += namei.o super.o symlink.o zlib_wrapper.o decompressor.o
194 squashfs-$(CONFIG_SQUASHFS_XATTRS) += xattr.o xattr_id.o
196 +squashfs-$(CONFIG_SQUASHFS_LZMA) += lzma_wrapper.o
197 diff -Nru a/fs/squashfs/squashfs.h b/fs/squashfs/squashfs.h
198 --- a/fs/squashfs/squashfs.h 2010-05-16 23:17:36.000000000 +0200
199 +++ b/fs/squashfs/squashfs.h 2010-05-17 14:57:45.310795600 +0200
203 extern const struct squashfs_decompressor squashfs_zlib_comp_ops;
205 +/* lzma wrapper.c */
206 +extern const struct squashfs_decompressor squashfs_lzma_comp_ops;
207 diff -Nru a/include/linux/decompress/bunzip2_mm.h b/include/linux/decompress/bunzip2_mm.h
208 --- a/include/linux/decompress/bunzip2_mm.h 1970-01-01 01:00:00.000000000 +0100
209 +++ b/include/linux/decompress/bunzip2_mm.h 2010-05-17 15:14:15.255545839 +0200
211 +#ifndef BUNZIP2_MM_H
212 +#define BUNZIP2_MM_H
215 +/* Code active when included from pre-boot environment: */
218 +/* Compile for initramfs/initrd code only */
220 +static void(*error)(char *m);
224 diff -Nru a/include/linux/decompress/inflate_mm.h b/include/linux/decompress/inflate_mm.h
225 --- a/include/linux/decompress/inflate_mm.h 1970-01-01 01:00:00.000000000 +0100
226 +++ b/include/linux/decompress/inflate_mm.h 2010-05-17 15:14:15.255545839 +0200
228 +#ifndef INFLATE_MM_H
229 +#define INFLATE_MM_H
232 +/* Code active when included from pre-boot environment: */
235 +/* Compile for initramfs/initrd code only */
237 +static void(*error)(char *m);
241 diff -Nru a/include/linux/decompress/mm.h b/include/linux/decompress/mm.h
242 --- a/include/linux/decompress/mm.h 2010-05-16 23:17:36.000000000 +0200
243 +++ b/include/linux/decompress/mm.h 2010-05-17 15:14:15.259546209 +0200
246 #define set_error_fn(x)
252 /* Code active when compiled standalone for use when loading ramdisk: */
254 #define large_malloc(a) vmalloc(a)
255 #define large_free(a) vfree(a)
257 -static void(*error)(char *m);
258 #define set_error_fn(x) error = x;
263 #include <linux/init.h>
264 diff -Nru a/include/linux/decompress/unlzma_mm.h b/include/linux/decompress/unlzma_mm.h
265 --- a/include/linux/decompress/unlzma_mm.h 1970-01-01 01:00:00.000000000 +0100
266 +++ b/include/linux/decompress/unlzma_mm.h 2010-05-17 15:13:10.802553245 +0200
273 +/* Code active when included from pre-boot environment: */
276 +#elif defined(CONFIG_DECOMPRESS_LZMA_NEEDED)
278 +/* Make it available to non initramfs/initrd code */
280 +#include <linux/module.h>
283 +/* Compile for initramfs/initrd code only */
288 diff -Nru a/include/linux/decompress/unlzo_mm.h b/include/linux/decompress/unlzo_mm.h
289 --- a/include/linux/decompress/unlzo_mm.h 1970-01-01 01:00:00.000000000 +0100
290 +++ b/include/linux/decompress/unlzo_mm.h 2010-05-17 15:14:15.259546209 +0200
296 +/* Code active when included from pre-boot environment: */
299 +/* Compile for initramfs/initrd code only */
301 +static void(*error)(char *m);
305 diff -Nru a/lib/decompress_bunzip2.c b/lib/decompress_bunzip2.c
306 --- a/lib/decompress_bunzip2.c 2010-05-16 23:17:36.000000000 +0200
307 +++ b/lib/decompress_bunzip2.c 2010-05-17 15:13:10.812574144 +0200
309 #include <linux/slab.h>
312 +#include <linux/decompress/bunzip2_mm.h>
313 #include <linux/decompress/mm.h>
316 diff -Nru a/lib/decompress_inflate.c b/lib/decompress_inflate.c
317 --- a/lib/decompress_inflate.c 2010-05-16 23:17:36.000000000 +0200
318 +++ b/lib/decompress_inflate.c 2010-05-17 15:13:10.815573687 +0200
323 +#include <linux/decompress/inflate_mm.h>
324 #include <linux/decompress/mm.h>
326 #define GZIP_IOBUF_SIZE (16*1024)
327 diff -Nru a/lib/decompress_unlzma.c b/lib/decompress_unlzma.c
328 --- a/lib/decompress_unlzma.c 2010-05-16 23:17:36.000000000 +0200
329 +++ b/lib/decompress_unlzma.c 2010-05-17 15:14:15.260574202 +0200
331 #include <linux/slab.h>
334 +#include <linux/decompress/unlzma_mm.h>
335 #include <linux/decompress/mm.h>
337 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
341 /* Called twice: once at startup and once in rc_normalize() */
342 -static void INIT rc_read(struct rc *rc)
343 +static void INIT rc_read(struct rc *rc, void(*error)(char *x))
345 rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
346 if (rc->buffer_size <= 0)
347 @@ -115,13 +116,13 @@
348 rc->range = 0xFFFFFFFF;
351 -static inline void INIT rc_init_code(struct rc *rc)
352 +static inline void INIT rc_init_code(struct rc *rc, void(*error)(char *x))
356 for (i = 0; i < 5; i++) {
357 if (rc->ptr >= rc->buffer_end)
359 + rc_read(rc, error);
360 rc->code = (rc->code << 8) | *rc->ptr++;
363 @@ -134,32 +135,33 @@
366 /* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
367 -static void INIT rc_do_normalize(struct rc *rc)
368 +static void INIT rc_do_normalize(struct rc *rc, void(*error)(char *x))
370 if (rc->ptr >= rc->buffer_end)
372 + rc_read(rc, error);
374 rc->code = (rc->code << 8) | *rc->ptr++;
376 -static inline void INIT rc_normalize(struct rc *rc)
377 +static inline void INIT rc_normalize(struct rc *rc, void(*error)(char *x))
379 if (rc->range < (1 << RC_TOP_BITS))
380 - rc_do_normalize(rc);
381 + rc_do_normalize(rc, error);
385 /* Why rc_is_bit_0_helper exists?
386 *Because we want to always expose (rc->code < rc->bound) to optimizer
388 -static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
389 +static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p,
390 + void (*error)(char *x))
393 + rc_normalize(rc, error);
394 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
397 -static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
398 +static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p, void(*error)(char *x))
400 - uint32_t t = rc_is_bit_0_helper(rc, p);
401 + uint32_t t = rc_is_bit_0_helper(rc, p, error);
408 /* Called 4 times in unlzma loop */
409 -static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
410 +static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol, void(*error)(char *x))
412 - if (rc_is_bit_0(rc, p)) {
413 + if (rc_is_bit_0(rc, p, error)) {
414 rc_update_bit_0(rc, p);
421 -static inline int INIT rc_direct_bit(struct rc *rc)
422 +static inline int INIT rc_direct_bit(struct rc *rc , void(*error)(char *x))
425 + rc_normalize(rc, error);
427 if (rc->code >= rc->range) {
428 rc->code -= rc->range;
429 @@ -204,13 +206,14 @@
432 static inline void INIT
433 -rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
434 +rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol,
435 + void(*error)(char *x))
441 - rc_get_bit(rc, p + *symbol, symbol);
442 + rc_get_bit(rc, p + *symbol, symbol, error);
443 *symbol -= 1 << num_levels;
447 static inline void INIT process_bit0(struct writer *wr, struct rc *rc,
448 struct cstate *cst, uint16_t *p,
449 int pos_state, uint16_t *prob,
450 - int lc, uint32_t literal_pos_mask) {
451 + int lc, uint32_t literal_pos_mask,
452 + void(*error)(char *x)) {
454 rc_update_bit_0(rc, prob);
455 prob = (p + LZMA_LITERAL +
458 bit = match_byte & 0x100;
459 prob_lit = prob + 0x100 + bit + mi;
460 - if (rc_get_bit(rc, prob_lit, &mi)) {
461 + if (rc_get_bit(rc, prob_lit, &mi, error)) {
468 uint16_t *prob_lit = prob + mi;
469 - rc_get_bit(rc, prob_lit, &mi);
470 + rc_get_bit(rc, prob_lit, &mi, error);
476 static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
477 struct cstate *cst, uint16_t *p,
478 - int pos_state, uint16_t *prob) {
479 + int pos_state, uint16_t *prob,
480 + void(*error)(char *x)) {
486 rc_update_bit_1(rc, prob);
487 prob = p + LZMA_IS_REP + cst->state;
488 - if (rc_is_bit_0(rc, prob)) {
489 + if (rc_is_bit_0(rc, prob, error)) {
490 rc_update_bit_0(rc, prob);
491 cst->rep3 = cst->rep2;
492 cst->rep2 = cst->rep1;
493 @@ -407,13 +412,13 @@
495 rc_update_bit_1(rc, prob);
496 prob = p + LZMA_IS_REP_G0 + cst->state;
497 - if (rc_is_bit_0(rc, prob)) {
498 + if (rc_is_bit_0(rc, prob, error)) {
499 rc_update_bit_0(rc, prob);
500 prob = (p + LZMA_IS_REP_0_LONG
502 LZMA_NUM_POS_BITS_MAX) +
504 - if (rc_is_bit_0(rc, prob)) {
505 + if (rc_is_bit_0(rc, prob, error)) {
506 rc_update_bit_0(rc, prob);
508 cst->state = cst->state < LZMA_NUM_LIT_STATES ?
509 @@ -428,13 +433,13 @@
511 rc_update_bit_1(rc, prob);
512 prob = p + LZMA_IS_REP_G1 + cst->state;
513 - if (rc_is_bit_0(rc, prob)) {
514 + if (rc_is_bit_0(rc, prob, error)) {
515 rc_update_bit_0(rc, prob);
516 distance = cst->rep1;
518 rc_update_bit_1(rc, prob);
519 prob = p + LZMA_IS_REP_G2 + cst->state;
520 - if (rc_is_bit_0(rc, prob)) {
521 + if (rc_is_bit_0(rc, prob, error)) {
522 rc_update_bit_0(rc, prob);
523 distance = cst->rep2;
528 prob_len = prob + LZMA_LEN_CHOICE;
529 - if (rc_is_bit_0(rc, prob_len)) {
530 + if (rc_is_bit_0(rc, prob_len, error)) {
531 rc_update_bit_0(rc, prob_len);
532 prob_len = (prob + LZMA_LEN_LOW
536 rc_update_bit_1(rc, prob_len);
537 prob_len = prob + LZMA_LEN_CHOICE_2;
538 - if (rc_is_bit_0(rc, prob_len)) {
539 + if (rc_is_bit_0(rc, prob_len, error)) {
540 rc_update_bit_0(rc, prob_len);
541 prob_len = (prob + LZMA_LEN_MID
547 - rc_bit_tree_decode(rc, prob_len, num_bits, &len);
548 + rc_bit_tree_decode(rc, prob_len, num_bits, &len, error);
551 if (cst->state < 4) {
553 << LZMA_NUM_POS_SLOT_BITS);
554 rc_bit_tree_decode(rc, prob,
555 LZMA_NUM_POS_SLOT_BITS,
558 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
560 num_bits = (pos_slot >> 1) - 1;
562 num_bits -= LZMA_NUM_ALIGN_BITS;
564 cst->rep0 = (cst->rep0 << 1) |
566 + rc_direct_bit(rc, error);
567 prob = p + LZMA_ALIGN;
568 cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
569 num_bits = LZMA_NUM_ALIGN_BITS;
574 - if (rc_get_bit(rc, prob + mi, &mi))
575 + if (rc_get_bit(rc, prob + mi, &mi, error))
579 @@ -531,12 +536,12 @@
583 -STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
584 +STATIC int INIT unlzma(unsigned char *buf, int in_len,
585 int(*fill)(void*, unsigned int),
586 int(*flush)(void*, unsigned int),
587 unsigned char *output,
589 - void(*error_fn)(char *x)
590 + void(*error)(char *x)
593 struct lzma_header header;
595 unsigned char *inbuf;
598 - set_error_fn(error_fn);
605 for (i = 0; i < sizeof(header); i++) {
606 if (rc.ptr >= rc.buffer_end)
608 + rc_read(&rc, error);
609 ((unsigned char *)&header)[i] = *rc.ptr++;
612 @@ -621,17 +624,17 @@
613 for (i = 0; i < num_probs; i++)
614 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
617 + rc_init_code(&rc, error);
619 while (get_pos(&wr) < header.dst_size) {
620 int pos_state = get_pos(&wr) & pos_state_mask;
621 uint16_t *prob = p + LZMA_IS_MATCH +
622 (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
623 - if (rc_is_bit_0(&rc, prob))
624 + if (rc_is_bit_0(&rc, prob, error))
625 process_bit0(&wr, &rc, &cst, p, pos_state, prob,
626 - lc, literal_pos_mask);
627 + lc, literal_pos_mask, error);
629 - process_bit1(&wr, &rc, &cst, p, pos_state, prob);
630 + process_bit1(&wr, &rc, &cst, p, pos_state, prob, error);
638 +#if defined(CONFIG_DECOMPRESS_LZMA_NEEDED) && !defined(PREBOOT)
639 +EXPORT_SYMBOL(unlzma);
643 STATIC int INIT decompress(unsigned char *buf, int in_len,
644 diff -Nru a/lib/decompress_unlzo.c b/lib/decompress_unlzo.c
645 --- a/lib/decompress_unlzo.c 2010-05-16 23:17:36.000000000 +0200
646 +++ b/lib/decompress_unlzo.c 2010-05-17 15:13:10.820554001 +0200
649 #include <linux/types.h>
650 #include <linux/lzo.h>
651 +#include <linux/decompress/unlzo_mm.h>
652 #include <linux/decompress/mm.h>
654 #include <linux/compiler.h>
655 diff -Nru a/lib/Kconfig b/lib/Kconfig
656 --- a/lib/Kconfig 2010-05-16 23:17:36.000000000 +0200
657 +++ b/lib/Kconfig 2010-05-17 15:13:10.809574529 +0200
659 select LZO_DECOMPRESS
662 +config DECOMPRESS_LZMA_NEEDED
666 # Generic allocator support is selected if needed
668 diff -Nru a/lib/Makefile b/lib/Makefile
669 --- a/lib/Makefile 2010-05-16 23:17:36.000000000 +0200
670 +++ b/lib/Makefile 2010-05-17 15:14:36.312796051 +0200
673 lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
674 lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
675 -lib-$(CONFIG_DECOMPRESS_LZMA) += decompress_unlzma.o
676 +obj-$(CONFIG_DECOMPRESS_LZMA) += decompress_unlzma.o
677 lib-$(CONFIG_DECOMPRESS_LZO) += decompress_unlzo.o
679 obj-$(CONFIG_TEXTSEARCH) += textsearch.o