linux/generic: add kernel 2.6.36 preliminary support
[openwrt.git] / target / linux / generic / patches-2.6.36 / 006-squashfs_add_lzma.patch
1 From f49e1efdd179d54e814ff2a8e8f469496583062c Mon Sep 17 00:00:00 2001
2 From: Phillip Lougher <phillip@lougher.demon.co.uk>
3 Date: Tue, 20 Oct 2009 10:54:36 +0100
4 Subject: [PATCH] Squashfs: add LZMA compression
5
6 Add support for LZMA compressed filesystems.  This is an initial
7 implementation.
8
9 Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
10 ---
11  fs/squashfs/Kconfig        |    5 ++
12  fs/squashfs/Makefile       |    1 +
13  fs/squashfs/decompressor.c |    4 +
14  fs/squashfs/lzma_wrapper.c |  151 ++++++++++++++++++++++++++++++++++++++++++++
15  fs/squashfs/squashfs.h     |    3 +
16  5 files changed, 164 insertions(+), 0 deletions(-)
17  create mode 100644 fs/squashfs/lzma_wrapper.c
18
19 --- a/fs/squashfs/Kconfig
20 +++ b/fs/squashfs/Kconfig
21 @@ -37,6 +37,11 @@ config SQUASHFS_XATTRS
22  
23           If unsure, say N.
24  
25 +config SQUASHFS_LZMA
26 +       bool "Include support for LZMA compressed file systems"
27 +       depends on SQUASHFS
28 +       select DECOMPRESS_LZMA
29 +
30  config SQUASHFS_EMBEDDED
31  
32         bool "Additional option for memory-constrained systems" 
33 --- a/fs/squashfs/Makefile
34 +++ b/fs/squashfs/Makefile
35 @@ -5,5 +5,4 @@
36  obj-$(CONFIG_SQUASHFS) += squashfs.o
37  squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o
38  squashfs-y += namei.o super.o symlink.o zlib_wrapper.o decompressor.o
39 -squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o
40 -squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o
41 +squashfs-$(CONFIG_SQUASHFS_LZMA) += lzma_wrapper.o
42 --- a/fs/squashfs/decompressor.c
43 +++ b/fs/squashfs/decompressor.c
44 @@ -53,8 +53,8 @@ static const struct squashfs_decompresso
45  static const struct squashfs_decompressor *decompressor[] = {
46         &squashfs_zlib_comp_ops,
47         &squashfs_lzma_unsupported_comp_ops,
48 -#ifdef CONFIG_SQUASHFS_LZO
49 -       &squashfs_lzo_comp_ops,
50 +#ifdef CONFIG_SQUASHFS_LZMA
51 +       &squashfs_lzma_comp_ops,
52  #else
53         &squashfs_lzo_unsupported_comp_ops,
54  #endif
55 --- /dev/null
56 +++ b/fs/squashfs/lzma_wrapper.c
57 @@ -0,0 +1,152 @@
58 +/*
59 + * Squashfs - a compressed read only filesystem for Linux
60 + *
61 + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
62 + * Phillip Lougher <phillip@lougher.demon.co.uk>
63 + *
64 + * This program is free software; you can redistribute it and/or
65 + * modify it under the terms of the GNU General Public License
66 + * as published by the Free Software Foundation; either version 2,
67 + * or (at your option) any later version.
68 + *
69 + * This program is distributed in the hope that it will be useful,
70 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
72 + * GNU General Public License for more details.
73 + *
74 + * You should have received a copy of the GNU General Public License
75 + * along with this program; if not, write to the Free Software
76 + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
77 + *
78 + * lzma_wrapper.c
79 + */
80 +
81 +#include <asm/unaligned.h>
82 +#include <linux/buffer_head.h>
83 +#include <linux/mutex.h>
84 +#include <linux/vmalloc.h>
85 +#include <linux/decompress/unlzma.h>
86 +#include <linux/slab.h>
87 +
88 +#include "squashfs_fs.h"
89 +#include "squashfs_fs_sb.h"
90 +#include "squashfs_fs_i.h"
91 +#include "squashfs.h"
92 +#include "decompressor.h"
93 +
94 +struct squashfs_lzma {
95 +       void    *input;
96 +       void    *output;
97 +};
98 +
99 +/* decompress_unlzma.c is currently non re-entrant... */
100 +DEFINE_MUTEX(lzma_mutex);
101 +
102 +/* decompress_unlzma.c doesn't provide any context in its callbacks... */
103 +static int lzma_error;
104 +
105 +static void error(char *m)
106 +{
107 +       ERROR("unlzma error: %s\n", m);
108 +       lzma_error = 1;
109 +}
110 +
111 +       
112 +static void *lzma_init(struct squashfs_sb_info *msblk)
113 +{
114 +       struct squashfs_lzma *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
115 +       if (stream == NULL)
116 +               goto failed;
117 +       stream->input = vmalloc(msblk->block_size);
118 +       if (stream->input == NULL)
119 +               goto failed;
120 +       stream->output = vmalloc(msblk->block_size);
121 +       if (stream->output == NULL)
122 +               goto failed2;
123 +
124 +       return stream;
125 +
126 +failed2:
127 +       vfree(stream->input);
128 +failed:
129 +       ERROR("failed to allocate lzma workspace\n");
130 +       kfree(stream);
131 +       return NULL;
132 +}
133 +
134 +
135 +static void lzma_free(void *strm)
136 +{
137 +       struct squashfs_lzma *stream = strm;
138 +
139 +       if (stream) {
140 +               vfree(stream->input);
141 +               vfree(stream->output);
142 +       }
143 +       kfree(stream);
144 +}
145 +
146 +
147 +static int lzma_uncompress(struct squashfs_sb_info *msblk, void **buffer,
148 +       struct buffer_head **bh, int b, int offset, int length, int srclength,
149 +       int pages)
150 +{
151 +       struct squashfs_lzma *stream = msblk->stream;
152 +       void *buff = stream->input;
153 +       int avail, i, bytes = length, res;
154 +
155 +       mutex_lock(&lzma_mutex);
156 +
157 +       for (i = 0; i < b; i++) {
158 +               wait_on_buffer(bh[i]);
159 +               if (!buffer_uptodate(bh[i]))
160 +                       goto block_release;
161 +
162 +               avail = min(bytes, msblk->devblksize - offset);
163 +               memcpy(buff, bh[i]->b_data + offset, avail);
164 +               buff += avail;
165 +               bytes -= avail;
166 +               offset = 0;
167 +               put_bh(bh[i]);
168 +       }
169 +
170 +       lzma_error = 0;
171 +       res = unlzma(stream->input, length, NULL, NULL, stream->output, NULL,
172 +                                                       error);
173 +       if (res || lzma_error)
174 +               goto failed;
175 +
176 +       /* uncompressed size is stored in the LZMA header (5 byte offset) */
177 +       res = bytes = get_unaligned_le32(stream->input + 5);
178 +       for (i = 0, buff = stream->output; bytes && i < pages; i++) {
179 +               avail = min_t(int, bytes, PAGE_CACHE_SIZE);
180 +               memcpy(buffer[i], buff, avail);
181 +               buff += avail;
182 +               bytes -= avail;
183 +       }
184 +       if (bytes)
185 +               goto failed;
186 +
187 +       mutex_unlock(&lzma_mutex);
188 +       return res;
189 +
190 +block_release:
191 +       for (; i < b; i++)
192 +               put_bh(bh[i]);
193 +
194 +failed:
195 +       mutex_unlock(&lzma_mutex);
196 +
197 +       ERROR("lzma decompression failed, data probably corrupt\n");
198 +       return -EIO;
199 +}
200 +
201 +const struct squashfs_decompressor squashfs_lzma_comp_ops = {
202 +       .init = lzma_init,
203 +       .free = lzma_free,
204 +       .decompress = lzma_uncompress,
205 +       .id = LZMA_COMPRESSION,
206 +       .name = "lzma",
207 +       .supported = 1
208 +};
209 +
210 --- a/fs/squashfs/squashfs.h
211 +++ b/fs/squashfs/squashfs.h
212 @@ -105,5 +105,5 @@ extern const struct xattr_handler *squas
213  /* zlib_wrapper.c */
214  extern const struct squashfs_decompressor squashfs_zlib_comp_ops;
215  
216 -/* lzo_wrapper.c */
217 -extern const struct squashfs_decompressor squashfs_lzo_comp_ops;
218 +/* lzma_wrapper.c */
219 +extern const struct squashfs_decompressor squashfs_lzma_comp_ops;