lzo_wrapper.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2010 LG Electronics
  5. * Chan Jeong <chan.jeong@lge.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * lzo_wrapper.c
  22. */
  23. #include <linux/mutex.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/slab.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/lzo.h>
  28. #include "squashfs_fs.h"
  29. #include "squashfs_fs_sb.h"
  30. #include "squashfs.h"
  31. #include "decompressor.h"
  32. #include "page_actor.h"
  33. struct squashfs_lzo {
  34. void *input;
  35. void *output;
  36. };
  37. static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
  38. {
  39. int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
  40. struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  41. if (stream == NULL)
  42. goto failed;
  43. stream->input = vmalloc(block_size);
  44. if (stream->input == NULL)
  45. goto failed;
  46. stream->output = vmalloc(block_size);
  47. if (stream->output == NULL)
  48. goto failed2;
  49. return stream;
  50. failed2:
  51. vfree(stream->input);
  52. failed:
  53. ERROR("Failed to allocate lzo workspace\n");
  54. kfree(stream);
  55. return ERR_PTR(-ENOMEM);
  56. }
  57. static void lzo_free(void *strm)
  58. {
  59. struct squashfs_lzo *stream = strm;
  60. if (stream) {
  61. vfree(stream->input);
  62. vfree(stream->output);
  63. }
  64. kfree(stream);
  65. }
  66. static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
  67. struct buffer_head **bh, int b, int offset, int length,
  68. struct squashfs_page_actor *output)
  69. {
  70. int res;
  71. size_t out_len = output->length;
  72. struct squashfs_lzo *stream = strm;
  73. squashfs_bh_to_buf(bh, b, stream->input, offset, length,
  74. msblk->devblksize);
  75. res = lzo1x_decompress_safe(stream->input, (size_t)length,
  76. stream->output, &out_len);
  77. if (res != LZO_E_OK)
  78. return -EIO;
  79. squashfs_buf_to_actor(stream->output, output, out_len);
  80. return out_len;
  81. }
  82. const struct squashfs_decompressor squashfs_lzo_comp_ops = {
  83. .init = lzo_init,
  84. .free = lzo_free,
  85. .decompress = lzo_uncompress,
  86. .id = LZO_COMPRESSION,
  87. .name = "lzo",
  88. .supported = 1
  89. };