remove the build INITRAMFS
[qi-kernel.git] / fs / squashfs / lzma_wrapper.c
blob9fa617d19e68e39cf9a0718bdaa67e567e47a76f
1 /*
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.
21 * lzma_wrapper.c
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"
33 #include "squashfs.h"
34 #include "decompressor.h"
36 struct squashfs_lzma {
37 void *input;
38 void *output;
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);
50 lzma_error = 1;
54 static void *lzma_init(struct squashfs_sb_info *msblk)
56 struct squashfs_lzma *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
57 if (stream == NULL)
58 goto failed;
59 stream->input = vmalloc(msblk->block_size);
60 if (stream->input == NULL)
61 goto failed;
62 stream->output = vmalloc(msblk->block_size);
63 if (stream->output == NULL)
64 goto failed2;
66 return stream;
68 failed2:
69 vfree(stream->input);
70 failed:
71 ERROR("failed to allocate lzma workspace\n");
72 kfree(stream);
73 return NULL;
77 static void lzma_free(void *strm)
79 struct squashfs_lzma *stream = strm;
81 if (stream) {
82 vfree(stream->input);
83 vfree(stream->output);
85 kfree(stream);
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,
91 int pages)
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]))
102 goto block_release;
104 avail = min(bytes, msblk->devblksize - offset);
105 memcpy(buff, bh[i]->b_data + offset, avail);
106 buff += avail;
107 bytes -= avail;
108 offset = 0;
109 put_bh(bh[i]);
112 lzma_error = 0;
113 res = unlzma(stream->input, length, NULL, NULL, stream->output, NULL,
114 error);
115 if (res || lzma_error)
116 goto failed;
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);
123 buff += avail;
124 bytes -= avail;
126 if (bytes)
127 goto failed;
129 mutex_unlock(&lzma_mutex);
130 return res;
132 block_release:
133 for (; i < b; i++)
134 put_bh(bh[i]);
136 failed:
137 mutex_unlock(&lzma_mutex);
139 ERROR("lzma decompression failed, data probably corrupt\n");
140 return -EIO;
143 const struct squashfs_decompressor squashfs_lzma_comp_ops = {
144 .init = lzma_init,
145 .free = lzma_free,
146 .decompress = lzma_uncompress,
147 .id = LZMA_COMPRESSION,
148 .name = "lzma",
149 .supported = 1