compr_zlib.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/slab.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());
  38. if (!def_strm.workspace) {
  39. printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
  40. return -ENOMEM;
  41. }
  42. D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
  43. inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
  44. if (!inf_strm.workspace) {
  45. printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
  46. vfree(def_strm.workspace);
  47. return -ENOMEM;
  48. }
  49. D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
  50. return 0;
  51. }
  52. static void free_workspaces(void)
  53. {
  54. vfree(def_strm.workspace);
  55. vfree(inf_strm.workspace);
  56. }
  57. #else
  58. #define alloc_workspaces() (0)
  59. #define free_workspaces() do { } while(0)
  60. #endif /* __KERNEL__ */
  61. static int jffs2_zlib_compress(unsigned char *data_in,
  62. unsigned char *cpage_out,
  63. uint32_t *sourcelen, uint32_t *dstlen,
  64. void *model)
  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. printk(KERN_WARNING "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((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
  82. D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
  83. def_strm.avail_in, def_strm.avail_out));
  84. ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
  85. D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
  86. def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
  87. if (ret != Z_OK) {
  88. D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
  89. zlib_deflateEnd(&def_strm);
  90. mutex_unlock(&deflate_mutex);
  91. return -1;
  92. }
  93. }
  94. def_strm.avail_out += STREAM_END_SPACE;
  95. def_strm.avail_in = 0;
  96. ret = zlib_deflate(&def_strm, Z_FINISH);
  97. zlib_deflateEnd(&def_strm);
  98. if (ret != Z_STREAM_END) {
  99. D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
  100. ret = -1;
  101. goto out;
  102. }
  103. if (def_strm.total_out >= def_strm.total_in) {
  104. D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
  105. def_strm.total_in, def_strm.total_out));
  106. ret = -1;
  107. goto out;
  108. }
  109. D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
  110. def_strm.total_in, def_strm.total_out));
  111. *dstlen = def_strm.total_out;
  112. *sourcelen = def_strm.total_in;
  113. ret = 0;
  114. out:
  115. mutex_unlock(&deflate_mutex);
  116. return ret;
  117. }
  118. static int jffs2_zlib_decompress(unsigned char *data_in,
  119. unsigned char *cpage_out,
  120. uint32_t srclen, uint32_t destlen,
  121. void *model)
  122. {
  123. int ret;
  124. int wbits = MAX_WBITS;
  125. mutex_lock(&inflate_mutex);
  126. inf_strm.next_in = data_in;
  127. inf_strm.avail_in = srclen;
  128. inf_strm.total_in = 0;
  129. inf_strm.next_out = cpage_out;
  130. inf_strm.avail_out = destlen;
  131. inf_strm.total_out = 0;
  132. /* If it's deflate, and it's got no preset dictionary, then
  133. we can tell zlib to skip the adler32 check. */
  134. if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  135. ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  136. !(((data_in[0]<<8) + data_in[1]) % 31)) {
  137. D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
  138. wbits = -((data_in[0] >> 4) + 8);
  139. inf_strm.next_in += 2;
  140. inf_strm.avail_in -= 2;
  141. } else {
  142. /* Let this remain D1 for now -- it should never happen */
  143. D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
  144. }
  145. if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
  146. printk(KERN_WARNING "inflateInit failed\n");
  147. mutex_unlock(&inflate_mutex);
  148. return 1;
  149. }
  150. while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
  151. ;
  152. if (ret != Z_STREAM_END) {
  153. printk(KERN_NOTICE "inflate returned %d\n", ret);
  154. }
  155. zlib_inflateEnd(&inf_strm);
  156. mutex_unlock(&inflate_mutex);
  157. return 0;
  158. }
  159. static struct jffs2_compressor jffs2_zlib_comp = {
  160. .priority = JFFS2_ZLIB_PRIORITY,
  161. .name = "zlib",
  162. .compr = JFFS2_COMPR_ZLIB,
  163. .compress = &jffs2_zlib_compress,
  164. .decompress = &jffs2_zlib_decompress,
  165. #ifdef JFFS2_ZLIB_DISABLED
  166. .disabled = 1,
  167. #else
  168. .disabled = 0,
  169. #endif
  170. };
  171. int __init jffs2_zlib_init(void)
  172. {
  173. int ret;
  174. ret = alloc_workspaces();
  175. if (ret)
  176. return ret;
  177. ret = jffs2_register_compressor(&jffs2_zlib_comp);
  178. if (ret)
  179. free_workspaces();
  180. return ret;
  181. }
  182. void jffs2_zlib_exit(void)
  183. {
  184. jffs2_unregister_compressor(&jffs2_zlib_comp);
  185. free_workspaces();
  186. }