lzo_wrapper.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. struct squashfs_lzo {
  33. void *input;
  34. void *output;
  35. };
  36. static void *lzo_init(struct squashfs_sb_info *msblk, void *buff, int len)
  37. {
  38. int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
  39. struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  40. if (stream == NULL)
  41. goto failed;
  42. stream->input = vmalloc(block_size);
  43. if (stream->input == NULL)
  44. goto failed;
  45. stream->output = vmalloc(block_size);
  46. if (stream->output == NULL)
  47. goto failed2;
  48. return stream;
  49. failed2:
  50. vfree(stream->input);
  51. failed:
  52. ERROR("Failed to allocate lzo workspace\n");
  53. kfree(stream);
  54. return ERR_PTR(-ENOMEM);
  55. }
  56. static void lzo_free(void *strm)
  57. {
  58. struct squashfs_lzo *stream = strm;
  59. if (stream) {
  60. vfree(stream->input);
  61. vfree(stream->output);
  62. }
  63. kfree(stream);
  64. }
  65. static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  66. struct buffer_head **bh, int b, int offset, int length, int srclength,
  67. int pages)
  68. {
  69. struct squashfs_lzo *stream = msblk->stream;
  70. void *buff = stream->input;
  71. int avail, i, bytes = length, res;
  72. size_t out_len = srclength;
  73. mutex_lock(&msblk->read_data_mutex);
  74. for (i = 0; i < b; i++) {
  75. wait_on_buffer(bh[i]);
  76. if (!buffer_uptodate(bh[i]))
  77. goto block_release;
  78. avail = min(bytes, msblk->devblksize - offset);
  79. memcpy(buff, bh[i]->b_data + offset, avail);
  80. buff += avail;
  81. bytes -= avail;
  82. offset = 0;
  83. put_bh(bh[i]);
  84. }
  85. res = lzo1x_decompress_safe(stream->input, (size_t)length,
  86. stream->output, &out_len);
  87. if (res != LZO_E_OK)
  88. goto failed;
  89. res = bytes = (int)out_len;
  90. for (i = 0, buff = stream->output; bytes && i < pages; i++) {
  91. avail = min_t(int, bytes, PAGE_CACHE_SIZE);
  92. memcpy(buffer[i], buff, avail);
  93. buff += avail;
  94. bytes -= avail;
  95. }
  96. mutex_unlock(&msblk->read_data_mutex);
  97. return res;
  98. block_release:
  99. for (; i < b; i++)
  100. put_bh(bh[i]);
  101. failed:
  102. mutex_unlock(&msblk->read_data_mutex);
  103. ERROR("lzo decompression failed, data probably corrupt\n");
  104. return -EIO;
  105. }
  106. const struct squashfs_decompressor squashfs_lzo_comp_ops = {
  107. .init = lzo_init,
  108. .free = lzo_free,
  109. .decompress = lzo_uncompress,
  110. .id = LZO_COMPRESSION,
  111. .name = "lzo",
  112. .supported = 1
  113. };