Commit f1a40359f8d8ba073257ed31a513e492621bcbc5

Authored by Phillip Lougher
1 parent e6a6d37955

Squashfs: factor out remaining zlib dependencies into separate wrapper file

Move zlib buffer init/destroy code into separate wrapper file.  Also
make zlib z_stream field a void * removing the need to include zlib.h
for most files.

Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>

Showing 14 changed files with 51 additions and 33 deletions Side-by-side Diff

... ... @@ -31,7 +31,6 @@
31 31 #include <linux/slab.h>
32 32 #include <linux/string.h>
33 33 #include <linux/buffer_head.h>
34   -#include <linux/zlib.h>
35 34  
36 35 #include "squashfs_fs.h"
37 36 #include "squashfs_fs_sb.h"
... ... @@ -51,7 +51,6 @@
51 51 #include <linux/sched.h>
52 52 #include <linux/spinlock.h>
53 53 #include <linux/wait.h>
54   -#include <linux/zlib.h>
55 54 #include <linux/pagemap.h>
56 55  
57 56 #include "squashfs_fs.h"
... ... @@ -30,7 +30,6 @@
30 30 #include <linux/fs.h>
31 31 #include <linux/vfs.h>
32 32 #include <linux/slab.h>
33   -#include <linux/zlib.h>
34 33  
35 34 #include "squashfs_fs.h"
36 35 #include "squashfs_fs_sb.h"
fs/squashfs/export.c
... ... @@ -39,7 +39,6 @@
39 39 #include <linux/vfs.h>
40 40 #include <linux/dcache.h>
41 41 #include <linux/exportfs.h>
42   -#include <linux/zlib.h>
43 42 #include <linux/slab.h>
44 43  
45 44 #include "squashfs_fs.h"
... ... @@ -47,7 +47,6 @@
47 47 #include <linux/string.h>
48 48 #include <linux/pagemap.h>
49 49 #include <linux/mutex.h>
50   -#include <linux/zlib.h>
51 50  
52 51 #include "squashfs_fs.h"
53 52 #include "squashfs_fs_sb.h"
fs/squashfs/fragment.c
... ... @@ -36,7 +36,6 @@
36 36 #include <linux/fs.h>
37 37 #include <linux/vfs.h>
38 38 #include <linux/slab.h>
39   -#include <linux/zlib.h>
40 39  
41 40 #include "squashfs_fs.h"
42 41 #include "squashfs_fs_sb.h"
... ... @@ -34,7 +34,6 @@
34 34 #include <linux/fs.h>
35 35 #include <linux/vfs.h>
36 36 #include <linux/slab.h>
37   -#include <linux/zlib.h>
38 37  
39 38 #include "squashfs_fs.h"
40 39 #include "squashfs_fs_sb.h"
... ... @@ -40,7 +40,6 @@
40 40  
41 41 #include <linux/fs.h>
42 42 #include <linux/vfs.h>
43   -#include <linux/zlib.h>
44 43  
45 44 #include "squashfs_fs.h"
46 45 #include "squashfs_fs_sb.h"
... ... @@ -57,7 +57,6 @@
57 57 #include <linux/slab.h>
58 58 #include <linux/string.h>
59 59 #include <linux/dcache.h>
60   -#include <linux/zlib.h>
61 60  
62 61 #include "squashfs_fs.h"
63 62 #include "squashfs_fs_sb.h"
fs/squashfs/squashfs.h
... ... @@ -71,6 +71,8 @@
71 71 extern int squashfs_read_inode(struct inode *, long long);
72 72  
73 73 /* zlib_wrapper.c */
  74 +extern void *squashfs_zlib_init(void);
  75 +extern void squashfs_zlib_free(void *);
74 76 extern int squashfs_zlib_uncompress(struct squashfs_sb_info *, void **,
75 77 struct buffer_head **, int, int, int, int, int);
76 78  
fs/squashfs/squashfs_fs_sb.h
... ... @@ -64,7 +64,7 @@
64 64 struct mutex read_data_mutex;
65 65 struct mutex meta_index_mutex;
66 66 struct meta_index *meta_index;
67   - z_stream stream;
  67 + void *stream;
68 68 __le64 *inode_lookup_table;
69 69 u64 inode_table;
70 70 u64 directory_table;
... ... @@ -35,7 +35,6 @@
35 35 #include <linux/pagemap.h>
36 36 #include <linux/init.h>
37 37 #include <linux/module.h>
38   -#include <linux/zlib.h>
39 38 #include <linux/magic.h>
40 39  
41 40 #include "squashfs_fs.h"
42 41  
... ... @@ -87,12 +86,9 @@
87 86 }
88 87 msblk = sb->s_fs_info;
89 88  
90   - msblk->stream.workspace = kmalloc(zlib_inflate_workspacesize(),
91   - GFP_KERNEL);
92   - if (msblk->stream.workspace == NULL) {
93   - ERROR("Failed to allocate zlib workspace\n");
  89 + msblk->stream = squashfs_zlib_init();
  90 + if (msblk->stream == NULL)
94 91 goto failure;
95   - }
96 92  
97 93 sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
98 94 if (sblk == NULL) {
99 95  
100 96  
... ... @@ -292,17 +288,17 @@
292 288 squashfs_cache_delete(msblk->block_cache);
293 289 squashfs_cache_delete(msblk->fragment_cache);
294 290 squashfs_cache_delete(msblk->read_page);
  291 + squashfs_zlib_free(msblk->stream);
295 292 kfree(msblk->inode_lookup_table);
296 293 kfree(msblk->fragment_index);
297 294 kfree(msblk->id_table);
298   - kfree(msblk->stream.workspace);
299 295 kfree(sb->s_fs_info);
300 296 sb->s_fs_info = NULL;
301 297 kfree(sblk);
302 298 return err;
303 299  
304 300 failure:
305   - kfree(msblk->stream.workspace);
  301 + squashfs_zlib_free(msblk->stream);
306 302 kfree(sb->s_fs_info);
307 303 sb->s_fs_info = NULL;
308 304 return -ENOMEM;
309 305  
... ... @@ -346,10 +342,10 @@
346 342 squashfs_cache_delete(sbi->block_cache);
347 343 squashfs_cache_delete(sbi->fragment_cache);
348 344 squashfs_cache_delete(sbi->read_page);
  345 + squashfs_zlib_free(sbi->stream);
349 346 kfree(sbi->id_table);
350 347 kfree(sbi->fragment_index);
351 348 kfree(sbi->meta_index);
352   - kfree(sbi->stream.workspace);
353 349 kfree(sb->s_fs_info);
354 350 sb->s_fs_info = NULL;
355 351 }
fs/squashfs/symlink.c
... ... @@ -36,7 +36,6 @@
36 36 #include <linux/slab.h>
37 37 #include <linux/string.h>
38 38 #include <linux/pagemap.h>
39   -#include <linux/zlib.h>
40 39  
41 40 #include "squashfs_fs.h"
42 41 #include "squashfs_fs_sb.h"
fs/squashfs/zlib_wrapper.c
... ... @@ -31,21 +31,51 @@
31 31 #include "squashfs_fs_i.h"
32 32 #include "squashfs.h"
33 33  
  34 +void *squashfs_zlib_init()
  35 +{
  36 + z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
  37 + if (stream == NULL)
  38 + goto failed;
  39 + stream->workspace = kmalloc(zlib_inflate_workspacesize(),
  40 + GFP_KERNEL);
  41 + if (stream->workspace == NULL)
  42 + goto failed;
  43 +
  44 + return stream;
  45 +
  46 +failed:
  47 + ERROR("Failed to allocate zlib workspace\n");
  48 + kfree(stream);
  49 + return NULL;
  50 +}
  51 +
  52 +
  53 +void squashfs_zlib_free(void *strm)
  54 +{
  55 + z_stream *stream = strm;
  56 +
  57 + if (stream)
  58 + kfree(stream->workspace);
  59 + kfree(stream);
  60 +}
  61 +
  62 +
34 63 int squashfs_zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
35 64 struct buffer_head **bh, int b, int offset, int length, int srclength,
36 65 int pages)
37 66 {
38 67 int zlib_err = 0, zlib_init = 0;
39 68 int avail, bytes, k = 0, page = 0;
  69 + z_stream *stream = msblk->stream;
40 70  
41 71 mutex_lock(&msblk->read_data_mutex);
42 72  
43   - msblk->stream.avail_out = 0;
44   - msblk->stream.avail_in = 0;
  73 + stream->avail_out = 0;
  74 + stream->avail_in = 0;
45 75  
46 76 bytes = length;
47 77 do {
48   - if (msblk->stream.avail_in == 0 && k < b) {
  78 + if (stream->avail_in == 0 && k < b) {
49 79 avail = min(bytes, msblk->devblksize - offset);
50 80 bytes -= avail;
51 81 wait_on_buffer(bh[k]);
52 82  
53 83  
... ... @@ -58,18 +88,18 @@
58 88 continue;
59 89 }
60 90  
61   - msblk->stream.next_in = bh[k]->b_data + offset;
62   - msblk->stream.avail_in = avail;
  91 + stream->next_in = bh[k]->b_data + offset;
  92 + stream->avail_in = avail;
63 93 offset = 0;
64 94 }
65 95  
66   - if (msblk->stream.avail_out == 0 && page < pages) {
67   - msblk->stream.next_out = buffer[page++];
68   - msblk->stream.avail_out = PAGE_CACHE_SIZE;
  96 + if (stream->avail_out == 0 && page < pages) {
  97 + stream->next_out = buffer[page++];
  98 + stream->avail_out = PAGE_CACHE_SIZE;
69 99 }
70 100  
71 101 if (!zlib_init) {
72   - zlib_err = zlib_inflateInit(&msblk->stream);
  102 + zlib_err = zlib_inflateInit(stream);
73 103 if (zlib_err != Z_OK) {
74 104 ERROR("zlib_inflateInit returned unexpected "
75 105 "result 0x%x, srclength %d\n",
76 106  
... ... @@ -79,9 +109,9 @@
79 109 zlib_init = 1;
80 110 }
81 111  
82   - zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
  112 + zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
83 113  
84   - if (msblk->stream.avail_in == 0 && k < b)
  114 + if (stream->avail_in == 0 && k < b)
85 115 put_bh(bh[k++]);
86 116 } while (zlib_err == Z_OK);
87 117  
88 118  
... ... @@ -90,14 +120,14 @@
90 120 goto release_mutex;
91 121 }
92 122  
93   - zlib_err = zlib_inflateEnd(&msblk->stream);
  123 + zlib_err = zlib_inflateEnd(stream);
94 124 if (zlib_err != Z_OK) {
95 125 ERROR("zlib_inflate error, data probably corrupt\n");
96 126 goto release_mutex;
97 127 }
98 128  
99 129 mutex_unlock(&msblk->read_data_mutex);
100   - return msblk->stream.total_out;
  130 + return stream->total_out;
101 131  
102 132 release_mutex:
103 133 mutex_unlock(&msblk->read_data_mutex);