compr_zlib.c 5.8 KB

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