001-squashfs_move_zlib_decomp.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. From 6c4419d997d4431bb62e73475cd6b084e83efbd1 Mon Sep 17 00:00:00 2001
  2. From: Phillip Lougher <[email protected]>
  3. Date: Tue, 22 Sep 2009 19:25:24 +0100
  4. Subject: [PATCH] Squashfs: move zlib decompression wrapper code into a separate file
  5. Signed-off-by: Phillip Lougher <[email protected]>
  6. ---
  7. fs/squashfs/Makefile | 2 +-
  8. fs/squashfs/block.c | 74 ++----------------------------
  9. fs/squashfs/squashfs.h | 4 ++
  10. fs/squashfs/zlib_wrapper.c | 109 ++++++++++++++++++++++++++++++++++++++++++++
  11. 4 files changed, 118 insertions(+), 71 deletions(-)
  12. create mode 100644 fs/squashfs/zlib_wrapper.c
  13. --- a/fs/squashfs/Makefile
  14. +++ b/fs/squashfs/Makefile
  15. @@ -4,4 +4,4 @@
  16. obj-$(CONFIG_SQUASHFS) += squashfs.o
  17. squashfs-y += block.o cache.o dir.o export.o file.o fragment.o id.o inode.o
  18. -squashfs-y += namei.o super.o symlink.o
  19. +squashfs-y += namei.o super.o symlink.o zlib_wrapper.o
  20. --- a/fs/squashfs/block.c
  21. +++ b/fs/squashfs/block.c
  22. @@ -29,7 +29,6 @@
  23. #include <linux/fs.h>
  24. #include <linux/vfs.h>
  25. #include <linux/slab.h>
  26. -#include <linux/mutex.h>
  27. #include <linux/string.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/zlib.h>
  30. @@ -153,72 +152,10 @@ int squashfs_read_data(struct super_bloc
  31. }
  32. if (compressed) {
  33. - int zlib_err = 0, zlib_init = 0;
  34. -
  35. - /*
  36. - * Uncompress block.
  37. - */
  38. -
  39. - mutex_lock(&msblk->read_data_mutex);
  40. -
  41. - msblk->stream.avail_out = 0;
  42. - msblk->stream.avail_in = 0;
  43. -
  44. - bytes = length;
  45. - do {
  46. - if (msblk->stream.avail_in == 0 && k < b) {
  47. - avail = min(bytes, msblk->devblksize - offset);
  48. - bytes -= avail;
  49. - wait_on_buffer(bh[k]);
  50. - if (!buffer_uptodate(bh[k]))
  51. - goto release_mutex;
  52. -
  53. - if (avail == 0) {
  54. - offset = 0;
  55. - put_bh(bh[k++]);
  56. - continue;
  57. - }
  58. -
  59. - msblk->stream.next_in = bh[k]->b_data + offset;
  60. - msblk->stream.avail_in = avail;
  61. - offset = 0;
  62. - }
  63. -
  64. - if (msblk->stream.avail_out == 0 && page < pages) {
  65. - msblk->stream.next_out = buffer[page++];
  66. - msblk->stream.avail_out = PAGE_CACHE_SIZE;
  67. - }
  68. -
  69. - if (!zlib_init) {
  70. - zlib_err = zlib_inflateInit(&msblk->stream);
  71. - if (zlib_err != Z_OK) {
  72. - ERROR("zlib_inflateInit returned"
  73. - " unexpected result 0x%x,"
  74. - " srclength %d\n", zlib_err,
  75. - srclength);
  76. - goto release_mutex;
  77. - }
  78. - zlib_init = 1;
  79. - }
  80. -
  81. - zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
  82. -
  83. - if (msblk->stream.avail_in == 0 && k < b)
  84. - put_bh(bh[k++]);
  85. - } while (zlib_err == Z_OK);
  86. -
  87. - if (zlib_err != Z_STREAM_END) {
  88. - ERROR("zlib_inflate error, data probably corrupt\n");
  89. - goto release_mutex;
  90. - }
  91. -
  92. - zlib_err = zlib_inflateEnd(&msblk->stream);
  93. - if (zlib_err != Z_OK) {
  94. - ERROR("zlib_inflate error, data probably corrupt\n");
  95. - goto release_mutex;
  96. - }
  97. - length = msblk->stream.total_out;
  98. - mutex_unlock(&msblk->read_data_mutex);
  99. + length = zlib_uncompress(msblk, buffer, bh, b, offset, length,
  100. + srclength, pages);
  101. + if (length < 0)
  102. + goto read_failure;
  103. } else {
  104. /*
  105. * Block is uncompressed.
  106. @@ -255,9 +192,6 @@ int squashfs_read_data(struct super_bloc
  107. kfree(bh);
  108. return length;
  109. -release_mutex:
  110. - mutex_unlock(&msblk->read_data_mutex);
  111. -
  112. block_release:
  113. for (; k < b; k++)
  114. put_bh(bh[k]);
  115. --- a/fs/squashfs/squashfs.h
  116. +++ b/fs/squashfs/squashfs.h
  117. @@ -70,6 +70,10 @@ extern struct inode *squashfs_iget(struc
  118. unsigned int);
  119. extern int squashfs_read_inode(struct inode *, long long);
  120. +/* zlib_wrapper.c */
  121. +extern int zlib_uncompress(struct squashfs_sb_info *, void **,
  122. + struct buffer_head **, int, int, int, int, int);
  123. +
  124. /*
  125. * Inodes and files operations
  126. */
  127. --- /dev/null
  128. +++ b/fs/squashfs/zlib_wrapper.c
  129. @@ -0,0 +1,109 @@
  130. +/*
  131. + * Squashfs - a compressed read only filesystem for Linux
  132. + *
  133. + * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  134. + * Phillip Lougher <[email protected]>
  135. + *
  136. + * This program is free software; you can redistribute it and/or
  137. + * modify it under the terms of the GNU General Public License
  138. + * as published by the Free Software Foundation; either version 2,
  139. + * or (at your option) any later version.
  140. + *
  141. + * This program is distributed in the hope that it will be useful,
  142. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  143. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  144. + * GNU General Public License for more details.
  145. + *
  146. + * You should have received a copy of the GNU General Public License
  147. + * along with this program; if not, write to the Free Software
  148. + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  149. + *
  150. + * zlib_wrapper.c
  151. + */
  152. +
  153. +
  154. +#include <linux/mutex.h>
  155. +#include <linux/buffer_head.h>
  156. +#include <linux/zlib.h>
  157. +
  158. +#include "squashfs_fs.h"
  159. +#include "squashfs_fs_sb.h"
  160. +#include "squashfs_fs_i.h"
  161. +#include "squashfs.h"
  162. +
  163. +int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  164. + struct buffer_head **bh, int b, int offset, int length, int srclength,
  165. + int pages)
  166. +{
  167. + int zlib_err = 0, zlib_init = 0;
  168. + int avail, bytes, k = 0, page = 0;
  169. +
  170. + mutex_lock(&msblk->read_data_mutex);
  171. +
  172. + msblk->stream.avail_out = 0;
  173. + msblk->stream.avail_in = 0;
  174. +
  175. + bytes = length;
  176. + do {
  177. + if (msblk->stream.avail_in == 0 && k < b) {
  178. + avail = min(bytes, msblk->devblksize - offset);
  179. + bytes -= avail;
  180. + wait_on_buffer(bh[k]);
  181. + if (!buffer_uptodate(bh[k]))
  182. + goto release_mutex;
  183. +
  184. + if (avail == 0) {
  185. + offset = 0;
  186. + put_bh(bh[k++]);
  187. + continue;
  188. + }
  189. +
  190. + msblk->stream.next_in = bh[k]->b_data + offset;
  191. + msblk->stream.avail_in = avail;
  192. + offset = 0;
  193. + }
  194. +
  195. + if (msblk->stream.avail_out == 0 && page < pages) {
  196. + msblk->stream.next_out = buffer[page++];
  197. + msblk->stream.avail_out = PAGE_CACHE_SIZE;
  198. + }
  199. +
  200. + if (!zlib_init) {
  201. + zlib_err = zlib_inflateInit(&msblk->stream);
  202. + if (zlib_err != Z_OK) {
  203. + ERROR("zlib_inflateInit returned unexpected "
  204. + "result 0x%x, srclength %d\n",
  205. + zlib_err, srclength);
  206. + goto release_mutex;
  207. + }
  208. + zlib_init = 1;
  209. + }
  210. +
  211. + zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
  212. +
  213. + if (msblk->stream.avail_in == 0 && k < b)
  214. + put_bh(bh[k++]);
  215. + } while (zlib_err == Z_OK);
  216. +
  217. + if (zlib_err != Z_STREAM_END) {
  218. + ERROR("zlib_inflate error, data probably corrupt\n");
  219. + goto release_mutex;
  220. + }
  221. +
  222. + zlib_err = zlib_inflateEnd(&msblk->stream);
  223. + if (zlib_err != Z_OK) {
  224. + ERROR("zlib_inflate error, data probably corrupt\n");
  225. + goto release_mutex;
  226. + }
  227. +
  228. + mutex_unlock(&msblk->read_data_mutex);
  229. + return msblk->stream.total_out;
  230. +
  231. +release_mutex:
  232. + mutex_unlock(&msblk->read_data_mutex);
  233. +
  234. + for (; k < b; k++)
  235. + put_bh(bh[k]);
  236. +
  237. + return -EIO;
  238. +}