compr_zlib.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * Created by David Woodhouse <dwmw2@infradead.org>
  8. *
  9. * For licensing information, see the file 'LICENCE' in this directory.
  10. *
  11. */
  12. #if !defined(__KERNEL__) && !defined(__ECOS)
  13. #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
  14. #endif
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/zlib.h>
  18. #include <linux/zutil.h>
  19. #include "nodelist.h"
  20. #include "compr.h"
  21. /* Plan: call deflate() with avail_in == *sourcelen,
  22. avail_out = *dstlen - 12 and flush == Z_FINISH.
  23. If it doesn't manage to finish, call it again with
  24. avail_in == 0 and avail_out set to the remaining 12
  25. bytes for it to clean up.
  26. Q: Is 12 bytes sufficient?
  27. */
  28. #define STREAM_END_SPACE 12
  29. static DEFINE_MUTEX(deflate_mutex);
  30. static DEFINE_MUTEX(inflate_mutex);
  31. static z_stream inf_strm, def_strm;
  32. #ifdef __KERNEL__ /* Linux-only */
  33. #include <linux/vmalloc.h>
  34. #include <linux/init.h>
  35. #include <linux/mutex.h>
  36. static int __init alloc_workspaces(void)
  37. {
  38. def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
  39. MAX_MEM_LEVEL));
  40. if (!def_strm.workspace)
  41. return -ENOMEM;
  42. jffs2_dbg(1, "Allocated %d bytes for deflate workspace\n",
  43. zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
  44. inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
  45. if (!inf_strm.workspace) {
  46. vfree(def_strm.workspace);
  47. return -ENOMEM;
  48. }
  49. jffs2_dbg(1, "Allocated %d bytes for inflate workspace\n",
  50. zlib_inflate_workspacesize());
  51. return 0;
  52. }
  53. static void free_workspaces(void)
  54. {
  55. vfree(def_strm.workspace);
  56. vfree(inf_strm.workspace);
  57. }
  58. #else
  59. #define alloc_workspaces() (0)
  60. #define free_workspaces() do { } while(0)
  61. #endif /* __KERNEL__ */
  62. static int jffs2_zlib_compress(unsigned char *data_in,
  63. unsigned char *cpage_out,
  64. uint32_t *sourcelen, uint32_t *dstlen)
  65. {
  66. int ret;
  67. if (*dstlen <= STREAM_END_SPACE)
  68. return -1;
  69. mutex_lock(&deflate_mutex);
  70. if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
  71. pr_warn("deflateInit failed\n");
  72. mutex_unlock(&deflate_mutex);
  73. return -1;
  74. }
  75. def_strm.next_in = data_in;
  76. def_strm.total_in = 0;
  77. def_strm.next_out = cpage_out;
  78. def_strm.total_out = 0;
  79. while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
  80. def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
  81. def_strm.avail_in = min_t(unsigned long,
  82. (*sourcelen-def_strm.total_in), def_strm.avail_out);
  83. jffs2_dbg(1, "calling deflate with avail_in %ld, avail_out %ld\n",
  84. def_strm.avail_in, def_strm.avail_out);
  85. ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
  86. jffs2_dbg(1, "deflate returned with avail_in %ld, avail_out %ld, total_in %ld, total_out %ld\n",
  87. def_strm.avail_in, def_strm.avail_out,
  88. def_strm.total_in, def_strm.total_out);
  89. if (ret != Z_OK) {
  90. jffs2_dbg(1, "deflate in loop returned %d\n", ret);
  91. zlib_deflateEnd(&def_strm);
  92. mutex_unlock(&deflate_mutex);
  93. return -1;
  94. }
  95. }
  96. def_strm.avail_out += STREAM_END_SPACE;
  97. def_strm.avail_in = 0;
  98. ret = zlib_deflate(&def_strm, Z_FINISH);
  99. zlib_deflateEnd(&def_strm);
  100. if (ret != Z_STREAM_END) {
  101. jffs2_dbg(1, "final deflate returned %d\n", ret);
  102. ret = -1;
  103. goto out;
  104. }
  105. if (def_strm.total_out >= def_strm.total_in) {
  106. jffs2_dbg(1, "zlib compressed %ld bytes into %ld; failing\n",
  107. def_strm.total_in, def_strm.total_out);
  108. ret = -1;
  109. goto out;
  110. }
  111. jffs2_dbg(1, "zlib compressed %ld bytes into %ld\n",
  112. def_strm.total_in, def_strm.total_out);
  113. *dstlen = def_strm.total_out;
  114. *sourcelen = def_strm.total_in;
  115. ret = 0;
  116. out:
  117. mutex_unlock(&deflate_mutex);
  118. return ret;
  119. }
  120. static int jffs2_zlib_decompress(unsigned char *data_in,
  121. unsigned char *cpage_out,
  122. uint32_t srclen, uint32_t destlen)
  123. {
  124. int ret;
  125. int wbits = MAX_WBITS;
  126. mutex_lock(&inflate_mutex);
  127. inf_strm.next_in = data_in;
  128. inf_strm.avail_in = srclen;
  129. inf_strm.total_in = 0;
  130. inf_strm.next_out = cpage_out;
  131. inf_strm.avail_out = destlen;
  132. inf_strm.total_out = 0;
  133. /* If it's deflate, and it's got no preset dictionary, then
  134. we can tell zlib to skip the adler32 check. */
  135. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  136. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  137. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  138. jffs2_dbg(2, "inflate skipping adler32\n");
  139. wbits = -((data_in[0] >> 4) + 8);
  140. inf_strm.next_in += 2;
  141. inf_strm.avail_in -= 2;
  142. } else {
  143. /* Let this remain D1 for now -- it should never happen */
  144. jffs2_dbg(1, "inflate not skipping adler32\n");
  145. }
  146. if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
  147. pr_warn("inflateInit failed\n");
  148. mutex_unlock(&inflate_mutex);
  149. return 1;
  150. }
  151. while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
  152. ;
  153. if (ret != Z_STREAM_END) {
  154. pr_notice("inflate returned %d\n", ret);
  155. }
  156. zlib_inflateEnd(&inf_strm);
  157. mutex_unlock(&inflate_mutex);
  158. return 0;
  159. }
  160. static struct jffs2_compressor jffs2_zlib_comp = {
  161. .priority = JFFS2_ZLIB_PRIORITY,
  162. .name = "zlib",
  163. .compr = JFFS2_COMPR_ZLIB,
  164. .compress = &jffs2_zlib_compress,
  165. .decompress = &jffs2_zlib_decompress,
  166. #ifdef JFFS2_ZLIB_DISABLED
  167. .disabled = 1,
  168. #else
  169. .disabled = 0,
  170. #endif
  171. };
  172. int __init jffs2_zlib_init(void)
  173. {
  174. int ret;
  175. ret = alloc_workspaces();
  176. if (ret)
  177. return ret;
  178. ret = jffs2_register_compressor(&jffs2_zlib_comp);
  179. if (ret)
  180. free_workspaces();
  181. return ret;
  182. }
  183. void jffs2_zlib_exit(void)
  184. {
  185. jffs2_unregister_compressor(&jffs2_zlib_comp);
  186. free_workspaces();
  187. }