compr_zlib.c 5.7 KB

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