2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <asm/unaligned.h>
25 #include <linux/buffer_head.h>
26 #include <linux/mutex.h>
27 #include <linux/vmalloc.h>
28 #include <linux/decompress/unlzma.h>
30 #include "squashfs_fs.h"
31 #include "squashfs_fs_sb.h"
32 #include "squashfs_fs_i.h"
34 #include "decompressor.h"
36 struct squashfs_lzma
{
41 /* decompress_unlzma.c is currently non re-entrant... */
42 DEFINE_MUTEX(lzma_mutex
);
44 /* decompress_unlzma.c doesn't provide any context in its callbacks... */
45 static int lzma_error
;
47 static void error(char *m
)
49 ERROR("unlzma error: %s\n", m
);
54 static void *lzma_init(struct squashfs_sb_info
*msblk
)
56 struct squashfs_lzma
*stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
59 stream
->input
= vmalloc(msblk
->block_size
);
60 if (stream
->input
== NULL
)
62 stream
->output
= vmalloc(msblk
->block_size
);
63 if (stream
->output
== NULL
)
71 ERROR("failed to allocate lzma workspace\n");
77 static void lzma_free(void *strm
)
79 struct squashfs_lzma
*stream
= strm
;
83 vfree(stream
->output
);
89 static int lzma_uncompress(struct squashfs_sb_info
*msblk
, void **buffer
,
90 struct buffer_head
**bh
, int b
, int offset
, int length
, int srclength
,
93 struct squashfs_lzma
*stream
= msblk
->stream
;
94 void *buff
= stream
->input
;
95 int avail
, i
, bytes
= length
, res
;
97 mutex_lock(&lzma_mutex
);
99 for (i
= 0; i
< b
; i
++) {
100 wait_on_buffer(bh
[i
]);
101 if (!buffer_uptodate(bh
[i
]))
104 avail
= min(bytes
, msblk
->devblksize
- offset
);
105 memcpy(buff
, bh
[i
]->b_data
+ offset
, avail
);
113 res
= unlzma(stream
->input
, length
, NULL
, NULL
, stream
->output
, NULL
,
115 if (res
|| lzma_error
)
118 /* uncompressed size is stored in the LZMA header (5 byte offset) */
119 res
= bytes
= get_unaligned_le32(stream
->input
+ 5);
120 for (i
= 0, buff
= stream
->output
; bytes
&& i
< pages
; i
++) {
121 avail
= min_t(int, bytes
, PAGE_CACHE_SIZE
);
122 memcpy(buffer
[i
], buff
, avail
);
129 mutex_unlock(&lzma_mutex
);
137 mutex_unlock(&lzma_mutex
);
139 ERROR("lzma decompression failed, data probably corrupt\n");
143 const struct squashfs_decompressor squashfs_lzma_comp_ops
= {
146 .decompress
= lzma_uncompress
,
147 .id
= LZMA_COMPRESSION
,