compr_zlib.c 5.6 KB

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