| 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 | @@ -26,6 +26,11 @@ config SQUASHFS |
| 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,3 +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_LZMA) += lzma_wrapper.o |
| 40 | --- a/fs/squashfs/decompressor.c |
| 41 | +++ b/fs/squashfs/decompressor.c |
| 42 | @@ -50,7 +50,11 @@ static const struct squashfs_decompresso |
| 43 | |
| 44 | static const struct squashfs_decompressor *decompressor[] = { |
| 45 | &squashfs_zlib_comp_ops, |
| 46 | +#ifdef CONFIG_SQUASHFS_LZMA |
| 47 | + &squashfs_lzma_comp_ops, |
| 48 | +#else |
| 49 | &squashfs_lzma_unsupported_comp_ops, |
| 50 | +#endif |
| 51 | &squashfs_lzo_unsupported_comp_ops, |
| 52 | &squashfs_unknown_comp_ops |
| 53 | }; |
| 54 | --- /dev/null |
| 55 | +++ b/fs/squashfs/lzma_wrapper.c |
| 56 | @@ -0,0 +1,151 @@ |
| 57 | +/* |
| 58 | + * Squashfs - a compressed read only filesystem for Linux |
| 59 | + * |
| 60 | + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
| 61 | + * Phillip Lougher <phillip@lougher.demon.co.uk> |
| 62 | + * |
| 63 | + * This program is free software; you can redistribute it and/or |
| 64 | + * modify it under the terms of the GNU General Public License |
| 65 | + * as published by the Free Software Foundation; either version 2, |
| 66 | + * or (at your option) any later version. |
| 67 | + * |
| 68 | + * This program is distributed in the hope that it will be useful, |
| 69 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 70 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 71 | + * GNU General Public License for more details. |
| 72 | + * |
| 73 | + * You should have received a copy of the GNU General Public License |
| 74 | + * along with this program; if not, write to the Free Software |
| 75 | + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 76 | + * |
| 77 | + * lzma_wrapper.c |
| 78 | + */ |
| 79 | + |
| 80 | +#include <asm/unaligned.h> |
| 81 | +#include <linux/buffer_head.h> |
| 82 | +#include <linux/mutex.h> |
| 83 | +#include <linux/vmalloc.h> |
| 84 | +#include <linux/decompress/unlzma.h> |
| 85 | + |
| 86 | +#include "squashfs_fs.h" |
| 87 | +#include "squashfs_fs_sb.h" |
| 88 | +#include "squashfs_fs_i.h" |
| 89 | +#include "squashfs.h" |
| 90 | +#include "decompressor.h" |
| 91 | + |
| 92 | +struct squashfs_lzma { |
| 93 | + void *input; |
| 94 | + void *output; |
| 95 | +}; |
| 96 | + |
| 97 | +/* decompress_unlzma.c is currently non re-entrant... */ |
| 98 | +DEFINE_MUTEX(lzma_mutex); |
| 99 | + |
| 100 | +/* decompress_unlzma.c doesn't provide any context in its callbacks... */ |
| 101 | +static int lzma_error; |
| 102 | + |
| 103 | +static void error(char *m) |
| 104 | +{ |
| 105 | + ERROR("unlzma error: %s\n", m); |
| 106 | + lzma_error = 1; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +static void *lzma_init(struct squashfs_sb_info *msblk) |
| 111 | +{ |
| 112 | + struct squashfs_lzma *stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 113 | + if (stream == NULL) |
| 114 | + goto failed; |
| 115 | + stream->input = vmalloc(msblk->block_size); |
| 116 | + if (stream->input == NULL) |
| 117 | + goto failed; |
| 118 | + stream->output = vmalloc(msblk->block_size); |
| 119 | + if (stream->output == NULL) |
| 120 | + goto failed2; |
| 121 | + |
| 122 | + return stream; |
| 123 | + |
| 124 | +failed2: |
| 125 | + vfree(stream->input); |
| 126 | +failed: |
| 127 | + ERROR("failed to allocate lzma workspace\n"); |
| 128 | + kfree(stream); |
| 129 | + return NULL; |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +static void lzma_free(void *strm) |
| 134 | +{ |
| 135 | + struct squashfs_lzma *stream = strm; |
| 136 | + |
| 137 | + if (stream) { |
| 138 | + vfree(stream->input); |
| 139 | + vfree(stream->output); |
| 140 | + } |
| 141 | + kfree(stream); |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +static int lzma_uncompress(struct squashfs_sb_info *msblk, void **buffer, |
| 146 | + struct buffer_head **bh, int b, int offset, int length, int srclength, |
| 147 | + int pages) |
| 148 | +{ |
| 149 | + struct squashfs_lzma *stream = msblk->stream; |
| 150 | + void *buff = stream->input; |
| 151 | + int avail, i, bytes = length, res; |
| 152 | + |
| 153 | + mutex_lock(&lzma_mutex); |
| 154 | + |
| 155 | + for (i = 0; i < b; i++) { |
| 156 | + wait_on_buffer(bh[i]); |
| 157 | + if (!buffer_uptodate(bh[i])) |
| 158 | + goto block_release; |
| 159 | + |
| 160 | + avail = min(bytes, msblk->devblksize - offset); |
| 161 | + memcpy(buff, bh[i]->b_data + offset, avail); |
| 162 | + buff += avail; |
| 163 | + bytes -= avail; |
| 164 | + offset = 0; |
| 165 | + put_bh(bh[i]); |
| 166 | + } |
| 167 | + |
| 168 | + lzma_error = 0; |
| 169 | + res = unlzma(stream->input, length, NULL, NULL, stream->output, NULL, |
| 170 | + error); |
| 171 | + if (res || lzma_error) |
| 172 | + goto failed; |
| 173 | + |
| 174 | + /* uncompressed size is stored in the LZMA header (5 byte offset) */ |
| 175 | + res = bytes = get_unaligned_le32(stream->input + 5); |
| 176 | + for (i = 0, buff = stream->output; bytes && i < pages; i++) { |
| 177 | + avail = min_t(int, bytes, PAGE_CACHE_SIZE); |
| 178 | + memcpy(buffer[i], buff, avail); |
| 179 | + buff += avail; |
| 180 | + bytes -= avail; |
| 181 | + } |
| 182 | + if (bytes) |
| 183 | + goto failed; |
| 184 | + |
| 185 | + mutex_unlock(&lzma_mutex); |
| 186 | + return res; |
| 187 | + |
| 188 | +block_release: |
| 189 | + for (; i < b; i++) |
| 190 | + put_bh(bh[i]); |
| 191 | + |
| 192 | +failed: |
| 193 | + mutex_unlock(&lzma_mutex); |
| 194 | + |
| 195 | + ERROR("lzma decompression failed, data probably corrupt\n"); |
| 196 | + return -EIO; |
| 197 | +} |
| 198 | + |
| 199 | +const struct squashfs_decompressor squashfs_lzma_comp_ops = { |
| 200 | + .init = lzma_init, |
| 201 | + .free = lzma_free, |
| 202 | + .decompress = lzma_uncompress, |
| 203 | + .id = LZMA_COMPRESSION, |
| 204 | + .name = "lzma", |
| 205 | + .supported = 1 |
| 206 | +}; |
| 207 | + |
| 208 | --- a/fs/squashfs/squashfs.h |
| 209 | +++ b/fs/squashfs/squashfs.h |
| 210 | @@ -94,3 +94,6 @@ extern const struct address_space_operat |
| 211 | |
| 212 | /* zlib_wrapper.c */ |
| 213 | extern const struct squashfs_decompressor squashfs_zlib_comp_ops; |
| 214 | + |
| 215 | +/* lzma wrapper.c */ |
| 216 | +extern const struct squashfs_decompressor squashfs_lzma_comp_ops; |
| 217 | |