1 From 327fbf47a419befc6bff74f3ca42d2b6f0841903 Mon Sep 17 00:00:00 2001
2 From: Phillip Lougher <phillip@lougher.demon.co.uk>
3 Date: Tue, 6 Oct 2009 04:04:15 +0100
4 Subject: [PATCH] Squashfs: add a decompressor framework
6 This adds a decompressor framework which allows multiple compression
7 algorithms to be cleanly supported.
9 Also update zlib wrapper and other code to use the new framework.
11 Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
13 fs/squashfs/Makefile | 2 +-
14 fs/squashfs/block.c | 6 ++--
15 fs/squashfs/decompressor.c | 58 ++++++++++++++++++++++++++++++++++++++++++
16 fs/squashfs/decompressor.h | 55 +++++++++++++++++++++++++++++++++++++++
17 fs/squashfs/squashfs.h | 14 +++++-----
18 fs/squashfs/squashfs_fs_sb.h | 41 +++++++++++++++--------------
19 fs/squashfs/super.c | 45 ++++++++++++++++++-------------
20 fs/squashfs/zlib_wrapper.c | 17 ++++++++++--
21 8 files changed, 185 insertions(+), 53 deletions(-)
22 create mode 100644 fs/squashfs/decompressor.c
23 create mode 100644 fs/squashfs/decompressor.h
25 --- a/fs/squashfs/Makefile
26 +++ b/fs/squashfs/Makefile
29 obj-$(CONFIG_SQUASHFS) += squashfs.o
30 squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o
31 -squashfs-y += namei.o super.o symlink.o zlib_wrapper.o
32 +squashfs-y += namei.o super.o symlink.o zlib_wrapper.o decompressor.o
33 --- a/fs/squashfs/block.c
34 +++ b/fs/squashfs/block.c
36 #include "squashfs_fs_sb.h"
37 #include "squashfs_fs_i.h"
40 +#include "decompressor.h"
42 * Read the metadata block length, this is stored in the first two
43 * bytes of the metadata block.
44 @@ -151,8 +151,8 @@ int squashfs_read_data(struct super_bloc
48 - length = zlib_uncompress(msblk, buffer, bh, b, offset, length,
50 + length = squashfs_decompress(msblk, buffer, bh, b, offset,
51 + length, srclength, pages);
56 +++ b/fs/squashfs/decompressor.c
59 + * Squashfs - a compressed read only filesystem for Linux
61 + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
62 + * Phillip Lougher <phillip@lougher.demon.co.uk>
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.
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.
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.
81 +#include <linux/types.h>
82 +#include <linux/mutex.h>
83 +#include <linux/buffer_head.h>
85 +#include "squashfs_fs.h"
86 +#include "squashfs_fs_sb.h"
87 +#include "squashfs_fs_i.h"
88 +#include "decompressor.h"
89 +#include "squashfs.h"
92 + * This file (and decompressor.h) implements a decompressor framework for
93 + * Squashfs, allowing multiple decompressors to be easily supported
96 +static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
97 + NULL, NULL, NULL, 0, "unknown", 0
100 +static const struct squashfs_decompressor *decompressor[] = {
101 + &squashfs_zlib_comp_ops,
102 + &squashfs_unknown_comp_ops
106 +const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
110 + for (i = 0; decompressor[i]->id; i++)
111 + if (id == decompressor[i]->id)
114 + return decompressor[i];
117 +++ b/fs/squashfs/decompressor.h
119 +#ifndef DECOMPRESSOR_H
120 +#define DECOMPRESSOR_H
122 + * Squashfs - a compressed read only filesystem for Linux
124 + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
125 + * Phillip Lougher <phillip@lougher.demon.co.uk>
127 + * This program is free software; you can redistribute it and/or
128 + * modify it under the terms of the GNU General Public License
129 + * as published by the Free Software Foundation; either version 2,
130 + * or (at your option) any later version.
132 + * This program is distributed in the hope that it will be useful,
133 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
134 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135 + * GNU General Public License for more details.
137 + * You should have received a copy of the GNU General Public License
138 + * along with this program; if not, write to the Free Software
139 + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
144 +struct squashfs_decompressor {
145 + void *(*init)(void);
146 + void (*free)(void *);
147 + int (*decompress)(struct squashfs_sb_info *, void **,
148 + struct buffer_head **, int, int, int, int, int);
154 +static inline void *squashfs_decompressor_init(struct squashfs_sb_info *msblk)
156 + return msblk->decompressor->init();
159 +static inline void squashfs_decompressor_free(struct squashfs_sb_info *msblk,
162 + if (msblk->decompressor)
163 + msblk->decompressor->free(s);
166 +static inline int squashfs_decompress(struct squashfs_sb_info *msblk,
167 + void **buffer, struct buffer_head **bh, int b, int offset, int length,
168 + int srclength, int pages)
170 + return msblk->decompressor->decompress(msblk, buffer, bh, b, offset,
171 + length, srclength, pages);
174 --- a/fs/squashfs/squashfs.h
175 +++ b/fs/squashfs/squashfs.h
176 @@ -51,6 +51,9 @@ extern struct squashfs_cache_entry *squa
178 extern int squashfs_read_table(struct super_block *, void *, u64, int);
180 +/* decompressor.c */
181 +extern const struct squashfs_decompressor *squashfs_lookup_decompressor(int);
184 extern __le64 *squashfs_read_inode_lookup_table(struct super_block *, u64,
186 @@ -70,14 +73,8 @@ extern struct inode *squashfs_iget(struc
188 extern int squashfs_read_inode(struct inode *, long long);
190 -/* zlib_wrapper.c */
191 -extern void *zlib_init(void);
192 -extern void zlib_free(void *);
193 -extern int zlib_uncompress(struct squashfs_sb_info *, void **,
194 - struct buffer_head **, int, int, int, int, int);
197 - * Inodes and files operations
198 + * Inodes, files and decompressor operations
202 @@ -94,3 +91,6 @@ extern const struct inode_operations squ
205 extern const struct address_space_operations squashfs_symlink_aops;
207 +/* zlib_wrapper.c */
208 +extern const struct squashfs_decompressor squashfs_zlib_comp_ops;
209 --- a/fs/squashfs/squashfs_fs_sb.h
210 +++ b/fs/squashfs/squashfs_fs_sb.h
211 @@ -52,25 +52,26 @@ struct squashfs_cache_entry {
214 struct squashfs_sb_info {
216 - int devblksize_log2;
217 - struct squashfs_cache *block_cache;
218 - struct squashfs_cache *fragment_cache;
219 - struct squashfs_cache *read_page;
220 - int next_meta_index;
222 - __le64 *fragment_index;
223 - unsigned int *fragment_index_2;
224 - struct mutex read_data_mutex;
225 - struct mutex meta_index_mutex;
226 - struct meta_index *meta_index;
228 - __le64 *inode_lookup_table;
230 - u64 directory_table;
231 - unsigned int block_size;
232 - unsigned short block_log;
233 - long long bytes_used;
234 - unsigned int inodes;
235 + const struct squashfs_decompressor *decompressor;
237 + int devblksize_log2;
238 + struct squashfs_cache *block_cache;
239 + struct squashfs_cache *fragment_cache;
240 + struct squashfs_cache *read_page;
241 + int next_meta_index;
243 + __le64 *fragment_index;
244 + unsigned int *fragment_index_2;
245 + struct mutex read_data_mutex;
246 + struct mutex meta_index_mutex;
247 + struct meta_index *meta_index;
249 + __le64 *inode_lookup_table;
251 + u64 directory_table;
252 + unsigned int block_size;
253 + unsigned short block_log;
254 + long long bytes_used;
255 + unsigned int inodes;
258 --- a/fs/squashfs/super.c
259 +++ b/fs/squashfs/super.c
261 #include "squashfs_fs_sb.h"
262 #include "squashfs_fs_i.h"
263 #include "squashfs.h"
264 +#include "decompressor.h"
266 static struct file_system_type squashfs_fs_type;
267 static const struct super_operations squashfs_super_ops;
269 -static int supported_squashfs_filesystem(short major, short minor, short comp)
270 +static const struct squashfs_decompressor *supported_squashfs_filesystem(short
271 + major, short minor, short id)
273 + const struct squashfs_decompressor *decompressor;
275 if (major < SQUASHFS_MAJOR) {
276 ERROR("Major/Minor mismatch, older Squashfs %d.%d "
277 "filesystems are unsupported\n", major, minor);
280 } else if (major > SQUASHFS_MAJOR || minor > SQUASHFS_MINOR) {
281 ERROR("Major/Minor mismatch, trying to mount newer "
282 "%d.%d filesystem\n", major, minor);
283 ERROR("Please update your kernel\n");
288 - if (comp != ZLIB_COMPRESSION)
290 + decompressor = squashfs_lookup_decompressor(id);
291 + if (!decompressor->supported) {
292 + ERROR("Filesystem uses \"%s\" compression. This is not "
293 + "supported\n", decompressor->name);
298 + return decompressor;
302 @@ -86,10 +94,6 @@ static int squashfs_fill_super(struct su
304 msblk = sb->s_fs_info;
306 - msblk->stream = zlib_init();
307 - if (msblk->stream == NULL)
310 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
312 ERROR("Failed to allocate squashfs_super_block\n");
313 @@ -116,25 +120,25 @@ static int squashfs_fill_super(struct su
319 /* Check it is a SQUASHFS superblock */
320 sb->s_magic = le32_to_cpu(sblk->s_magic);
321 if (sb->s_magic != SQUASHFS_MAGIC) {
323 ERROR("Can't find a SQUASHFS superblock on %s\n",
324 bdevname(sb->s_bdev, b));
329 - /* Check the MAJOR & MINOR versions and compression type */
330 - err = supported_squashfs_filesystem(le16_to_cpu(sblk->s_major),
331 + /* Check the MAJOR & MINOR versions and lookup compression type */
332 + msblk->decompressor = supported_squashfs_filesystem(
333 + le16_to_cpu(sblk->s_major),
334 le16_to_cpu(sblk->s_minor),
335 le16_to_cpu(sblk->compression));
337 + if (msblk->decompressor == NULL)
343 * Check if there's xattrs in the filesystem. These are not
344 * supported in this version, so warn that they will be ignored.
345 @@ -201,6 +205,10 @@ static int squashfs_fill_super(struct su
349 + msblk->stream = squashfs_decompressor_init(msblk);
350 + if (msblk->stream == NULL)
353 msblk->block_cache = squashfs_cache_init("metadata",
354 SQUASHFS_CACHED_BLKS, SQUASHFS_METADATA_SIZE);
355 if (msblk->block_cache == NULL)
356 @@ -288,7 +296,7 @@ failed_mount:
357 squashfs_cache_delete(msblk->block_cache);
358 squashfs_cache_delete(msblk->fragment_cache);
359 squashfs_cache_delete(msblk->read_page);
360 - zlib_free(msblk->stream);
361 + squashfs_decompressor_free(msblk, msblk->stream);
362 kfree(msblk->inode_lookup_table);
363 kfree(msblk->fragment_index);
364 kfree(msblk->id_table);
365 @@ -298,7 +306,6 @@ failed_mount:
369 - zlib_free(msblk->stream);
370 kfree(sb->s_fs_info);
371 sb->s_fs_info = NULL;
373 @@ -342,7 +349,7 @@ static void squashfs_put_super(struct su
374 squashfs_cache_delete(sbi->block_cache);
375 squashfs_cache_delete(sbi->fragment_cache);
376 squashfs_cache_delete(sbi->read_page);
377 - zlib_free(sbi->stream);
378 + squashfs_decompressor_free(sbi, sbi->stream);
379 kfree(sbi->id_table);
380 kfree(sbi->fragment_index);
381 kfree(sbi->meta_index);
382 --- a/fs/squashfs/zlib_wrapper.c
383 +++ b/fs/squashfs/zlib_wrapper.c
385 #include "squashfs_fs_sb.h"
386 #include "squashfs_fs_i.h"
387 #include "squashfs.h"
388 +#include "decompressor.h"
391 +static void *zlib_init(void)
393 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
395 @@ -50,7 +51,7 @@ failed:
399 -void zlib_free(void *strm)
400 +static void zlib_free(void *strm)
402 z_stream *stream = strm;
404 @@ -60,7 +61,7 @@ void zlib_free(void *strm)
408 -int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
409 +static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
410 struct buffer_head **bh, int b, int offset, int length, int srclength,
413 @@ -137,3 +138,13 @@ release_mutex:
418 +const struct squashfs_decompressor squashfs_zlib_comp_ops = {
421 + .decompress = zlib_uncompress,
422 + .id = ZLIB_COMPRESSION,