Skip to content

Commit 9508c6b

Browse files
committed
Squashfs: Refactor decompressor interface and code
The decompressor interface and code was written from the point of view of single-threaded operation. In doing so it mixed a lot of single-threaded implementation specific aspects into the decompressor code and elsewhere which makes it difficult to seamlessly support multiple different decompressor implementations. This patch does the following: 1. It removes compressor_options parsing from the decompressor init() function. This allows the decompressor init() function to be dynamically called to instantiate multiple decompressors, without the compressor options needing to be read and parsed each time. 2. It moves threading and all sleeping operations out of the decompressors. In doing so, it makes the decompressors non-blocking wrappers which only deal with interfacing with the decompressor implementation. 3. It splits decompressor.[ch] into decompressor generic functions in decompressor.[ch], and moves the single threaded decompressor implementation into decompressor_single.c. The result of this patch is Squashfs should now be able to support multiple decompressors by adding new decompressor_xxx.c files with specialised implementations of the functions in decompressor_single.c Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk> Reviewed-by: Minchan Kim <minchan@kernel.org>
1 parent 959f585 commit 9508c6b

11 files changed

Lines changed: 216 additions & 136 deletions

File tree

‎fs/squashfs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
obj-$(CONFIG_SQUASHFS) += squashfs.o
66
squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o
7-
squashfs-y += namei.o super.o symlink.o decompressor.o
7+
squashfs-y += namei.o super.o symlink.o decompressor.o decompressor_single.o
88
squashfs-$(CONFIG_SQUASHFS_XATTR) += xattr.o xattr_id.o
99
squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o
1010
squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o

‎fs/squashfs/block.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
9393
struct buffer_head **bh;
9494
int offset = index & ((1 << msblk->devblksize_log2) - 1);
9595
u64 cur_index = index >> msblk->devblksize_log2;
96-
int bytes, compressed, b = 0, k = 0, page = 0, avail;
96+
int bytes, compressed, b = 0, k = 0, page = 0, avail, i;
9797

9898
bh = kcalloc(((srclength + msblk->devblksize - 1)
9999
>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
@@ -158,6 +158,12 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
158158
ll_rw_block(READ, b - 1, bh + 1);
159159
}
160160

161+
for (i = 0; i < b; i++) {
162+
wait_on_buffer(bh[i]);
163+
if (!buffer_uptodate(bh[i]))
164+
goto block_release;
165+
}
166+
161167
if (compressed) {
162168
length = squashfs_decompress(msblk, buffer, bh, b, offset,
163169
length, srclength, pages);
@@ -172,9 +178,6 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
172178
for (bytes = length; k < b; k++) {
173179
in = min(bytes, msblk->devblksize - offset);
174180
bytes -= in;
175-
wait_on_buffer(bh[k]);
176-
if (!buffer_uptodate(bh[k]))
177-
goto block_release;
178181
while (in) {
179182
if (pg_offset == PAGE_CACHE_SIZE) {
180183
page++;

‎fs/squashfs/decompressor.c‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@
3737
*/
3838

3939
static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = {
40-
NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
40+
NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
4141
};
4242

4343
#ifndef CONFIG_SQUASHFS_LZO
4444
static const struct squashfs_decompressor squashfs_lzo_comp_ops = {
45-
NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
45+
NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
4646
};
4747
#endif
4848

4949
#ifndef CONFIG_SQUASHFS_XZ
5050
static const struct squashfs_decompressor squashfs_xz_comp_ops = {
51-
NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
51+
NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
5252
};
5353
#endif
5454

5555
#ifndef CONFIG_SQUASHFS_ZLIB
5656
static const struct squashfs_decompressor squashfs_zlib_comp_ops = {
57-
NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
57+
NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
5858
};
5959
#endif
6060

6161
static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
62-
NULL, NULL, NULL, 0, "unknown", 0
62+
NULL, NULL, NULL, NULL, 0, "unknown", 0
6363
};
6464

6565
static const struct squashfs_decompressor *decompressor[] = {
@@ -83,34 +83,51 @@ const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
8383
}
8484

8585

86-
void *squashfs_decompressor_init(struct super_block *sb, unsigned short flags)
86+
static void *get_comp_opts(struct super_block *sb, unsigned short flags)
8787
{
8888
struct squashfs_sb_info *msblk = sb->s_fs_info;
89-
void *strm, *buffer = NULL;
89+
void *buffer = NULL, *comp_opts;
9090
int length = 0;
9191

9292
/*
9393
* Read decompressor specific options from file system if present
9494
*/
9595
if (SQUASHFS_COMP_OPTS(flags)) {
9696
buffer = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
97-
if (buffer == NULL)
98-
return ERR_PTR(-ENOMEM);
97+
if (buffer == NULL) {
98+
comp_opts = ERR_PTR(-ENOMEM);
99+
goto out;
100+
}
99101

100102
length = squashfs_read_data(sb, &buffer,
101103
sizeof(struct squashfs_super_block), 0, NULL,
102-
PAGE_CACHE_SIZE, 1);
104+
PAGE_CACHE_SIZE, 1);
103105

104106
if (length < 0) {
105-
strm = ERR_PTR(length);
106-
goto finished;
107+
comp_opts = ERR_PTR(length);
108+
goto out;
107109
}
108110
}
109111

110-
strm = msblk->decompressor->init(msblk, buffer, length);
112+
comp_opts = squashfs_comp_opts(msblk, buffer, length);
111113

112-
finished:
114+
out:
113115
kfree(buffer);
116+
return comp_opts;
117+
}
118+
119+
120+
void *squashfs_decompressor_setup(struct super_block *sb, unsigned short flags)
121+
{
122+
struct squashfs_sb_info *msblk = sb->s_fs_info;
123+
void *stream, *comp_opts = get_comp_opts(sb, flags);
124+
125+
if (IS_ERR(comp_opts))
126+
return comp_opts;
127+
128+
stream = squashfs_decompressor_create(msblk, comp_opts);
129+
if (IS_ERR(stream))
130+
kfree(comp_opts);
114131

115-
return strm;
132+
return stream;
116133
}

‎fs/squashfs/decompressor.h‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,21 @@
2424
*/
2525

2626
struct squashfs_decompressor {
27-
void *(*init)(struct squashfs_sb_info *, void *, int);
27+
void *(*init)(struct squashfs_sb_info *, void *);
28+
void *(*comp_opts)(struct squashfs_sb_info *, void *, int);
2829
void (*free)(void *);
29-
int (*decompress)(struct squashfs_sb_info *, void **,
30+
int (*decompress)(struct squashfs_sb_info *, void *, void **,
3031
struct buffer_head **, int, int, int, int, int);
3132
int id;
3233
char *name;
3334
int supported;
3435
};
3536

36-
static inline void squashfs_decompressor_free(struct squashfs_sb_info *msblk,
37-
void *s)
37+
static inline void *squashfs_comp_opts(struct squashfs_sb_info *msblk,
38+
void *buff, int length)
3839
{
39-
if (msblk->decompressor)
40-
msblk->decompressor->free(s);
41-
}
42-
43-
static inline int squashfs_decompress(struct squashfs_sb_info *msblk,
44-
void **buffer, struct buffer_head **bh, int b, int offset, int length,
45-
int srclength, int pages)
46-
{
47-
return msblk->decompressor->decompress(msblk, buffer, bh, b, offset,
48-
length, srclength, pages);
40+
return msblk->decompressor->comp_opts ?
41+
msblk->decompressor->comp_opts(msblk, buff, length) : NULL;
4942
}
5043

5144
#ifdef CONFIG_SQUASHFS_XZ

‎fs/squashfs/decompressor_single.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2013
3+
* Phillip Lougher <phillip@squashfs.org.uk>
4+
*
5+
* This work is licensed under the terms of the GNU GPL, version 2. See
6+
* the COPYING file in the top-level directory.
7+
*/
8+
9+
#include <linux/types.h>
10+
#include <linux/mutex.h>
11+
#include <linux/slab.h>
12+
#include <linux/buffer_head.h>
13+
14+
#include "squashfs_fs.h"
15+
#include "squashfs_fs_sb.h"
16+
#include "decompressor.h"
17+
#include "squashfs.h"
18+
19+
/*
20+
* This file implements single-threaded decompression in the
21+
* decompressor framework
22+
*/
23+
24+
struct squashfs_stream {
25+
void *stream;
26+
struct mutex mutex;
27+
};
28+
29+
void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
30+
void *comp_opts)
31+
{
32+
struct squashfs_stream *stream;
33+
int err = -ENOMEM;
34+
35+
stream = kmalloc(sizeof(*stream), GFP_KERNEL);
36+
if (stream == NULL)
37+
goto out;
38+
39+
stream->stream = msblk->decompressor->init(msblk, comp_opts);
40+
if (IS_ERR(stream->stream)) {
41+
err = PTR_ERR(stream->stream);
42+
goto out;
43+
}
44+
45+
kfree(comp_opts);
46+
mutex_init(&stream->mutex);
47+
return stream;
48+
49+
out:
50+
kfree(stream);
51+
return ERR_PTR(err);
52+
}
53+
54+
void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
55+
{
56+
struct squashfs_stream *stream = msblk->stream;
57+
58+
if (stream) {
59+
msblk->decompressor->free(stream->stream);
60+
kfree(stream);
61+
}
62+
}
63+
64+
int squashfs_decompress(struct squashfs_sb_info *msblk,
65+
void **buffer, struct buffer_head **bh, int b, int offset, int length,
66+
int srclength, int pages)
67+
{
68+
int res;
69+
struct squashfs_stream *stream = msblk->stream;
70+
71+
mutex_lock(&stream->mutex);
72+
res = msblk->decompressor->decompress(msblk, stream->stream, buffer,
73+
bh, b, offset, length, srclength, pages);
74+
mutex_unlock(&stream->mutex);
75+
76+
if (res < 0)
77+
ERROR("%s decompression failed, data probably corrupt\n",
78+
msblk->decompressor->name);
79+
80+
return res;
81+
}
82+
83+
int squashfs_max_decompressors(void)
84+
{
85+
return 1;
86+
}

‎fs/squashfs/lzo_wrapper.c‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct squashfs_lzo {
3737
void *output;
3838
};
3939

40-
static void *lzo_init(struct squashfs_sb_info *msblk, void *buff, int len)
40+
static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
4141
{
4242
int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
4343

@@ -74,22 +74,16 @@ static void lzo_free(void *strm)
7474
}
7575

7676

77-
static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer,
78-
struct buffer_head **bh, int b, int offset, int length, int srclength,
79-
int pages)
77+
static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
78+
void **buffer, struct buffer_head **bh, int b, int offset, int length,
79+
int srclength, int pages)
8080
{
81-
struct squashfs_lzo *stream = msblk->stream;
81+
struct squashfs_lzo *stream = strm;
8282
void *buff = stream->input;
8383
int avail, i, bytes = length, res;
8484
size_t out_len = srclength;
8585

86-
mutex_lock(&msblk->read_data_mutex);
87-
8886
for (i = 0; i < b; i++) {
89-
wait_on_buffer(bh[i]);
90-
if (!buffer_uptodate(bh[i]))
91-
goto block_release;
92-
9387
avail = min(bytes, msblk->devblksize - offset);
9488
memcpy(buff, bh[i]->b_data + offset, avail);
9589
buff += avail;
@@ -111,17 +105,9 @@ static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer,
111105
bytes -= avail;
112106
}
113107

114-
mutex_unlock(&msblk->read_data_mutex);
115108
return res;
116109

117-
block_release:
118-
for (; i < b; i++)
119-
put_bh(bh[i]);
120-
121110
failed:
122-
mutex_unlock(&msblk->read_data_mutex);
123-
124-
ERROR("lzo decompression failed, data probably corrupt\n");
125111
return -EIO;
126112
}
127113

‎fs/squashfs/squashfs.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ extern void *squashfs_read_table(struct super_block *, u64, int);
4848

4949
/* decompressor.c */
5050
extern const struct squashfs_decompressor *squashfs_lookup_decompressor(int);
51-
extern void *squashfs_decompressor_init(struct super_block *, unsigned short);
51+
extern void *squashfs_decompressor_setup(struct super_block *, unsigned short);
52+
53+
/* decompressor_xxx.c */
54+
extern void *squashfs_decompressor_create(struct squashfs_sb_info *, void *);
55+
extern void squashfs_decompressor_destroy(struct squashfs_sb_info *);
56+
extern int squashfs_decompress(struct squashfs_sb_info *, void **,
57+
struct buffer_head **, int, int, int, int, int);
58+
extern int squashfs_max_decompressors(void);
5259

5360
/* export.c */
5461
extern __le64 *squashfs_read_inode_lookup_table(struct super_block *, u64, u64,

‎fs/squashfs/squashfs_fs_sb.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ struct squashfs_sb_info {
6363
__le64 *id_table;
6464
__le64 *fragment_index;
6565
__le64 *xattr_id_table;
66-
struct mutex read_data_mutex;
6766
struct mutex meta_index_mutex;
6867
struct meta_index *meta_index;
69-
void *stream;
68+
struct squashfs_stream *stream;
7069
__le64 *inode_lookup_table;
7170
u64 inode_table;
7271
u64 directory_table;

‎fs/squashfs/super.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
9898
msblk->devblksize = sb_min_blocksize(sb, SQUASHFS_DEVBLK_SIZE);
9999
msblk->devblksize_log2 = ffz(~msblk->devblksize);
100100

101-
mutex_init(&msblk->read_data_mutex);
102101
mutex_init(&msblk->meta_index_mutex);
103102

104103
/*
@@ -206,13 +205,14 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
206205
goto failed_mount;
207206

208207
/* Allocate read_page block */
209-
msblk->read_page = squashfs_cache_init("data", 1, msblk->block_size);
208+
msblk->read_page = squashfs_cache_init("data",
209+
squashfs_max_decompressors(), msblk->block_size);
210210
if (msblk->read_page == NULL) {
211211
ERROR("Failed to allocate read_page block\n");
212212
goto failed_mount;
213213
}
214214

215-
msblk->stream = squashfs_decompressor_init(sb, flags);
215+
msblk->stream = squashfs_decompressor_setup(sb, flags);
216216
if (IS_ERR(msblk->stream)) {
217217
err = PTR_ERR(msblk->stream);
218218
msblk->stream = NULL;
@@ -336,7 +336,7 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
336336
squashfs_cache_delete(msblk->block_cache);
337337
squashfs_cache_delete(msblk->fragment_cache);
338338
squashfs_cache_delete(msblk->read_page);
339-
squashfs_decompressor_free(msblk, msblk->stream);
339+
squashfs_decompressor_destroy(msblk);
340340
kfree(msblk->inode_lookup_table);
341341
kfree(msblk->fragment_index);
342342
kfree(msblk->id_table);
@@ -383,7 +383,7 @@ static void squashfs_put_super(struct super_block *sb)
383383
squashfs_cache_delete(sbi->block_cache);
384384
squashfs_cache_delete(sbi->fragment_cache);
385385
squashfs_cache_delete(sbi->read_page);
386-
squashfs_decompressor_free(sbi, sbi->stream);
386+
squashfs_decompressor_destroy(sbi);
387387
kfree(sbi->id_table);
388388
kfree(sbi->fragment_index);
389389
kfree(sbi->meta_index);

0 commit comments

Comments
 (0)