002-squashfs_factor_out_remaining_zlib.patch 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. From 37c44e85fd49676ec15ccaeea065662c1fbcda7d Mon Sep 17 00:00:00 2001
  2. From: Phillip Lougher <[email protected]>
  3. Date: Wed, 23 Sep 2009 19:04:49 +0100
  4. Subject: [PATCH] Squashfs: Factor out remaining zlib dependencies into separate wrapper file
  5. Move zlib buffer init/destroy code into separate wrapper file. Also
  6. make zlib z_stream field a void * removing the need to include zlib.h
  7. for most files.
  8. Signed-off-by: Phillip Lougher <[email protected]>
  9. ---
  10. fs/squashfs/block.c | 1 -
  11. fs/squashfs/cache.c | 1 -
  12. fs/squashfs/dir.c | 1 -
  13. fs/squashfs/export.c | 1 -
  14. fs/squashfs/file.c | 1 -
  15. fs/squashfs/fragment.c | 1 -
  16. fs/squashfs/id.c | 1 -
  17. fs/squashfs/inode.c | 1 -
  18. fs/squashfs/namei.c | 1 -
  19. fs/squashfs/squashfs.h | 2 +
  20. fs/squashfs/squashfs_fs_sb.h | 2 +-
  21. fs/squashfs/super.c | 14 +++------
  22. fs/squashfs/symlink.c | 1 -
  23. fs/squashfs/zlib_wrapper.c | 56 ++++++++++++++++++++++++++++++++---------
  24. 14 files changed, 51 insertions(+), 33 deletions(-)
  25. --- a/fs/squashfs/block.c
  26. +++ b/fs/squashfs/block.c
  27. @@ -31,7 +31,6 @@
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/buffer_head.h>
  31. -#include <linux/zlib.h>
  32. #include "squashfs_fs.h"
  33. #include "squashfs_fs_sb.h"
  34. --- a/fs/squashfs/cache.c
  35. +++ b/fs/squashfs/cache.c
  36. @@ -51,7 +51,6 @@
  37. #include <linux/sched.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/wait.h>
  40. -#include <linux/zlib.h>
  41. #include <linux/pagemap.h>
  42. #include "squashfs_fs.h"
  43. --- a/fs/squashfs/dir.c
  44. +++ b/fs/squashfs/dir.c
  45. @@ -30,7 +30,6 @@
  46. #include <linux/fs.h>
  47. #include <linux/vfs.h>
  48. #include <linux/slab.h>
  49. -#include <linux/zlib.h>
  50. #include "squashfs_fs.h"
  51. #include "squashfs_fs_sb.h"
  52. --- a/fs/squashfs/export.c
  53. +++ b/fs/squashfs/export.c
  54. @@ -39,7 +39,6 @@
  55. #include <linux/vfs.h>
  56. #include <linux/dcache.h>
  57. #include <linux/exportfs.h>
  58. -#include <linux/zlib.h>
  59. #include <linux/slab.h>
  60. #include "squashfs_fs.h"
  61. --- a/fs/squashfs/file.c
  62. +++ b/fs/squashfs/file.c
  63. @@ -47,7 +47,6 @@
  64. #include <linux/string.h>
  65. #include <linux/pagemap.h>
  66. #include <linux/mutex.h>
  67. -#include <linux/zlib.h>
  68. #include "squashfs_fs.h"
  69. #include "squashfs_fs_sb.h"
  70. --- a/fs/squashfs/fragment.c
  71. +++ b/fs/squashfs/fragment.c
  72. @@ -36,7 +36,6 @@
  73. #include <linux/fs.h>
  74. #include <linux/vfs.h>
  75. #include <linux/slab.h>
  76. -#include <linux/zlib.h>
  77. #include "squashfs_fs.h"
  78. #include "squashfs_fs_sb.h"
  79. --- a/fs/squashfs/id.c
  80. +++ b/fs/squashfs/id.c
  81. @@ -34,7 +34,6 @@
  82. #include <linux/fs.h>
  83. #include <linux/vfs.h>
  84. #include <linux/slab.h>
  85. -#include <linux/zlib.h>
  86. #include "squashfs_fs.h"
  87. #include "squashfs_fs_sb.h"
  88. --- a/fs/squashfs/inode.c
  89. +++ b/fs/squashfs/inode.c
  90. @@ -40,7 +40,6 @@
  91. #include <linux/fs.h>
  92. #include <linux/vfs.h>
  93. -#include <linux/zlib.h>
  94. #include "squashfs_fs.h"
  95. #include "squashfs_fs_sb.h"
  96. --- a/fs/squashfs/namei.c
  97. +++ b/fs/squashfs/namei.c
  98. @@ -57,7 +57,6 @@
  99. #include <linux/slab.h>
  100. #include <linux/string.h>
  101. #include <linux/dcache.h>
  102. -#include <linux/zlib.h>
  103. #include "squashfs_fs.h"
  104. #include "squashfs_fs_sb.h"
  105. --- a/fs/squashfs/squashfs.h
  106. +++ b/fs/squashfs/squashfs.h
  107. @@ -71,6 +71,8 @@ extern struct inode *squashfs_iget(struc
  108. extern int squashfs_read_inode(struct inode *, long long);
  109. /* zlib_wrapper.c */
  110. +extern void *zlib_init(void);
  111. +extern void zlib_free(void *);
  112. extern int zlib_uncompress(struct squashfs_sb_info *, void **,
  113. struct buffer_head **, int, int, int, int, int);
  114. --- a/fs/squashfs/squashfs_fs_sb.h
  115. +++ b/fs/squashfs/squashfs_fs_sb.h
  116. @@ -64,7 +64,7 @@ struct squashfs_sb_info {
  117. struct mutex read_data_mutex;
  118. struct mutex meta_index_mutex;
  119. struct meta_index *meta_index;
  120. - z_stream stream;
  121. + void *stream;
  122. __le64 *inode_lookup_table;
  123. u64 inode_table;
  124. u64 directory_table;
  125. --- a/fs/squashfs/super.c
  126. +++ b/fs/squashfs/super.c
  127. @@ -35,7 +35,6 @@
  128. #include <linux/pagemap.h>
  129. #include <linux/init.h>
  130. #include <linux/module.h>
  131. -#include <linux/zlib.h>
  132. #include <linux/magic.h>
  133. #include "squashfs_fs.h"
  134. @@ -87,12 +86,9 @@ static int squashfs_fill_super(struct su
  135. }
  136. msblk = sb->s_fs_info;
  137. - msblk->stream.workspace = kmalloc(zlib_inflate_workspacesize(),
  138. - GFP_KERNEL);
  139. - if (msblk->stream.workspace == NULL) {
  140. - ERROR("Failed to allocate zlib workspace\n");
  141. + msblk->stream = zlib_init();
  142. + if (msblk->stream == NULL)
  143. goto failure;
  144. - }
  145. sblk = kzalloc(sizeof(*sblk), GFP_KERNEL);
  146. if (sblk == NULL) {
  147. @@ -292,17 +288,17 @@ failed_mount:
  148. squashfs_cache_delete(msblk->block_cache);
  149. squashfs_cache_delete(msblk->fragment_cache);
  150. squashfs_cache_delete(msblk->read_page);
  151. + zlib_free(msblk->stream);
  152. kfree(msblk->inode_lookup_table);
  153. kfree(msblk->fragment_index);
  154. kfree(msblk->id_table);
  155. - kfree(msblk->stream.workspace);
  156. kfree(sb->s_fs_info);
  157. sb->s_fs_info = NULL;
  158. kfree(sblk);
  159. return err;
  160. failure:
  161. - kfree(msblk->stream.workspace);
  162. + zlib_free(msblk->stream);
  163. kfree(sb->s_fs_info);
  164. sb->s_fs_info = NULL;
  165. return -ENOMEM;
  166. @@ -346,10 +342,10 @@ static void squashfs_put_super(struct su
  167. squashfs_cache_delete(sbi->block_cache);
  168. squashfs_cache_delete(sbi->fragment_cache);
  169. squashfs_cache_delete(sbi->read_page);
  170. + zlib_free(sbi->stream);
  171. kfree(sbi->id_table);
  172. kfree(sbi->fragment_index);
  173. kfree(sbi->meta_index);
  174. - kfree(sbi->stream.workspace);
  175. kfree(sb->s_fs_info);
  176. sb->s_fs_info = NULL;
  177. }
  178. --- a/fs/squashfs/symlink.c
  179. +++ b/fs/squashfs/symlink.c
  180. @@ -36,7 +36,6 @@
  181. #include <linux/slab.h>
  182. #include <linux/string.h>
  183. #include <linux/pagemap.h>
  184. -#include <linux/zlib.h>
  185. #include "squashfs_fs.h"
  186. #include "squashfs_fs_sb.h"
  187. --- a/fs/squashfs/zlib_wrapper.c
  188. +++ b/fs/squashfs/zlib_wrapper.c
  189. @@ -31,21 +31,51 @@
  190. #include "squashfs_fs_i.h"
  191. #include "squashfs.h"
  192. +void *zlib_init()
  193. +{
  194. + z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
  195. + if (stream == NULL)
  196. + goto failed;
  197. + stream->workspace = kmalloc(zlib_inflate_workspacesize(),
  198. + GFP_KERNEL);
  199. + if (stream->workspace == NULL)
  200. + goto failed;
  201. +
  202. + return stream;
  203. +
  204. +failed:
  205. + ERROR("Failed to allocate zlib workspace\n");
  206. + kfree(stream);
  207. + return NULL;
  208. +}
  209. +
  210. +
  211. +void zlib_free(void *strm)
  212. +{
  213. + z_stream *stream = strm;
  214. +
  215. + if (stream)
  216. + kfree(stream->workspace);
  217. + kfree(stream);
  218. +}
  219. +
  220. +
  221. int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  222. struct buffer_head **bh, int b, int offset, int length, int srclength,
  223. int pages)
  224. {
  225. int zlib_err = 0, zlib_init = 0;
  226. int avail, bytes, k = 0, page = 0;
  227. + z_stream *stream = msblk->stream;
  228. mutex_lock(&msblk->read_data_mutex);
  229. - msblk->stream.avail_out = 0;
  230. - msblk->stream.avail_in = 0;
  231. + stream->avail_out = 0;
  232. + stream->avail_in = 0;
  233. bytes = length;
  234. do {
  235. - if (msblk->stream.avail_in == 0 && k < b) {
  236. + if (stream->avail_in == 0 && k < b) {
  237. avail = min(bytes, msblk->devblksize - offset);
  238. bytes -= avail;
  239. wait_on_buffer(bh[k]);
  240. @@ -58,18 +88,18 @@ int zlib_uncompress(struct squashfs_sb_i
  241. continue;
  242. }
  243. - msblk->stream.next_in = bh[k]->b_data + offset;
  244. - msblk->stream.avail_in = avail;
  245. + stream->next_in = bh[k]->b_data + offset;
  246. + stream->avail_in = avail;
  247. offset = 0;
  248. }
  249. - if (msblk->stream.avail_out == 0 && page < pages) {
  250. - msblk->stream.next_out = buffer[page++];
  251. - msblk->stream.avail_out = PAGE_CACHE_SIZE;
  252. + if (stream->avail_out == 0 && page < pages) {
  253. + stream->next_out = buffer[page++];
  254. + stream->avail_out = PAGE_CACHE_SIZE;
  255. }
  256. if (!zlib_init) {
  257. - zlib_err = zlib_inflateInit(&msblk->stream);
  258. + zlib_err = zlib_inflateInit(stream);
  259. if (zlib_err != Z_OK) {
  260. ERROR("zlib_inflateInit returned unexpected "
  261. "result 0x%x, srclength %d\n",
  262. @@ -79,9 +109,9 @@ int zlib_uncompress(struct squashfs_sb_i
  263. zlib_init = 1;
  264. }
  265. - zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
  266. + zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
  267. - if (msblk->stream.avail_in == 0 && k < b)
  268. + if (stream->avail_in == 0 && k < b)
  269. put_bh(bh[k++]);
  270. } while (zlib_err == Z_OK);
  271. @@ -90,14 +120,14 @@ int zlib_uncompress(struct squashfs_sb_i
  272. goto release_mutex;
  273. }
  274. - zlib_err = zlib_inflateEnd(&msblk->stream);
  275. + zlib_err = zlib_inflateEnd(stream);
  276. if (zlib_err != Z_OK) {
  277. ERROR("zlib_inflate error, data probably corrupt\n");
  278. goto release_mutex;
  279. }
  280. mutex_unlock(&msblk->read_data_mutex);
  281. - return msblk->stream.total_out;
  282. + return stream->total_out;
  283. release_mutex:
  284. mutex_unlock(&msblk->read_data_mutex);