compr_lzo.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2007 Nokia Corporation. All rights reserved.
  5. *
  6. * Created by Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.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. printk(KERN_WARNING "Failed to allocate lzo deflate workspace\n");
  32. free_workspace();
  33. return -ENOMEM;
  34. }
  35. return 0;
  36. }
  37. static int jffs2_lzo_compress(unsigned char *data_in, unsigned char *cpage_out,
  38. uint32_t *sourcelen, uint32_t *dstlen, void *model)
  39. {
  40. size_t compress_size;
  41. int ret;
  42. mutex_lock(&deflate_mutex);
  43. ret = lzo1x_1_compress(data_in, *sourcelen, lzo_compress_buf, &compress_size, lzo_mem);
  44. if (ret != LZO_E_OK)
  45. goto fail;
  46. if (compress_size > *dstlen)
  47. goto fail;
  48. memcpy(cpage_out, lzo_compress_buf, compress_size);
  49. mutex_unlock(&deflate_mutex);
  50. *dstlen = compress_size;
  51. return 0;
  52. fail:
  53. mutex_unlock(&deflate_mutex);
  54. return -1;
  55. }
  56. static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
  57. uint32_t srclen, uint32_t destlen, void *model)
  58. {
  59. size_t dl = destlen;
  60. int ret;
  61. ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl);
  62. if (ret != LZO_E_OK || dl != destlen)
  63. return -1;
  64. return 0;
  65. }
  66. static struct jffs2_compressor jffs2_lzo_comp = {
  67. .priority = JFFS2_LZO_PRIORITY,
  68. .name = "lzo",
  69. .compr = JFFS2_COMPR_LZO,
  70. .compress = &jffs2_lzo_compress,
  71. .decompress = &jffs2_lzo_decompress,
  72. .disabled = 0,
  73. };
  74. int __init jffs2_lzo_init(void)
  75. {
  76. int ret;
  77. ret = alloc_workspace();
  78. if (ret < 0)
  79. return ret;
  80. ret = jffs2_register_compressor(&jffs2_lzo_comp);
  81. if (ret)
  82. free_workspace();
  83. return ret;
  84. }
  85. void jffs2_lzo_exit(void)
  86. {
  87. jffs2_unregister_compressor(&jffs2_lzo_comp);
  88. free_workspace();
  89. }