compr_zlib.c 5.8 KB

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