compr_lzo.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2007 Nokia Corporation. All rights reserved.
  5. * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * Created by Richard Purdie <rpurdie@openedhand.com>
  8. *
  9. * For licensing information, see the file 'LICENCE' in this directory.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/init.h>
  16. #include <linux/lzo.h>
  17. #include "compr.h"
  18. static void *lzo_mem;
  19. static void *lzo_compress_buf;
  20. static DEFINE_MUTEX(deflate_mutex); /* for lzo_mem and lzo_compress_buf */
  21. static void free_workspace(void)
  22. {
  23. vfree(lzo_mem);
  24. vfree(lzo_compress_buf);
  25. }
  26. static int __init alloc_workspace(void)
  27. {
  28. lzo_mem = vmalloc(LZO1X_MEM_COMPRESS);
  29. lzo_compress_buf = vmalloc(lzo1x_worst_compress(PAGE_SIZE));
  30. if (!lzo_mem || !lzo_compress_buf) {
  31. free_workspace();
  32. return -ENOMEM;
  33. }
  34. return 0;
  35. }
  36. static int jffs2_lzo_compress(unsigned char *data_in, unsigned char *cpage_out,
  37. uint32_t *sourcelen, uint32_t *dstlen)
  38. {
  39. size_t compress_size;
  40. int ret;
  41. mutex_lock(&deflate_mutex);
  42. ret = lzo1x_1_compress(data_in, *sourcelen, lzo_compress_buf, &compress_size, lzo_mem);
  43. if (ret != LZO_E_OK)
  44. goto fail;
  45. if (compress_size > *dstlen)
  46. goto fail;
  47. memcpy(cpage_out, lzo_compress_buf, compress_size);
  48. mutex_unlock(&deflate_mutex);
  49. *dstlen = compress_size;
  50. return 0;
  51. fail:
  52. mutex_unlock(&deflate_mutex);
  53. return -1;
  54. }
  55. static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
  56. uint32_t srclen, uint32_t destlen)
  57. {
  58. size_t dl = destlen;
  59. int ret;
  60. ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl);
  61. if (ret != LZO_E_OK || dl != destlen)
  62. return -1;
  63. return 0;
  64. }
  65. static struct jffs2_compressor jffs2_lzo_comp = {
  66. .priority = JFFS2_LZO_PRIORITY,
  67. .name = "lzo",
  68. .compr = JFFS2_COMPR_LZO,
  69. .compress = &jffs2_lzo_compress,
  70. .decompress = &jffs2_lzo_decompress,
  71. .disabled = 0,
  72. };
  73. int __init jffs2_lzo_init(void)
  74. {
  75. int ret;
  76. ret = alloc_workspace();
  77. if (ret < 0)
  78. return ret;
  79. ret = jffs2_register_compressor(&jffs2_lzo_comp);
  80. if (ret)
  81. free_workspace();
  82. return ret;
  83. }
  84. void jffs2_lzo_exit(void)
  85. {
  86. jffs2_unregister_compressor(&jffs2_lzo_comp);
  87. free_workspace();
  88. }