compr_zlib.c 5.7 KB

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