lz4_wrapper.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2013, 2014
  3. * Phillip Lougher <phillip@squashfs.org.uk>
  4. *
  5. * This work is licensed under the terms of the GNU GPL, version 2. See
  6. * the COPYING file in the top-level directory.
  7. */
  8. #include <linux/buffer_head.h>
  9. #include <linux/mutex.h>
  10. #include <linux/slab.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/lz4.h>
  13. #include "squashfs_fs.h"
  14. #include "squashfs_fs_sb.h"
  15. #include "squashfs.h"
  16. #include "decompressor.h"
  17. #include "page_actor.h"
  18. #define LZ4_LEGACY 1
  19. struct lz4_comp_opts {
  20. __le32 version;
  21. __le32 flags;
  22. };
  23. struct squashfs_lz4 {
  24. void *input;
  25. void *output;
  26. };
  27. static void *lz4_comp_opts(struct squashfs_sb_info *msblk,
  28. void *buff, int len)
  29. {
  30. struct lz4_comp_opts *comp_opts = buff;
  31. /* LZ4 compressed filesystems always have compression options */
  32. if (comp_opts == NULL || len < sizeof(*comp_opts))
  33. return ERR_PTR(-EIO);
  34. if (le32_to_cpu(comp_opts->version) != LZ4_LEGACY) {
  35. /* LZ4 format currently used by the kernel is the 'legacy'
  36. * format */
  37. ERROR("Unknown LZ4 version\n");
  38. return ERR_PTR(-EINVAL);
  39. }
  40. return NULL;
  41. }
  42. static void *lz4_init(struct squashfs_sb_info *msblk, void *buff)
  43. {
  44. int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
  45. struct squashfs_lz4 *stream;
  46. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  47. if (stream == NULL)
  48. goto failed;
  49. stream->input = vmalloc(block_size);
  50. if (stream->input == NULL)
  51. goto failed2;
  52. stream->output = vmalloc(block_size);
  53. if (stream->output == NULL)
  54. goto failed3;
  55. return stream;
  56. failed3:
  57. vfree(stream->input);
  58. failed2:
  59. kfree(stream);
  60. failed:
  61. ERROR("Failed to initialise LZ4 decompressor\n");
  62. return ERR_PTR(-ENOMEM);
  63. }
  64. static void lz4_free(void *strm)
  65. {
  66. struct squashfs_lz4 *stream = strm;
  67. if (stream) {
  68. vfree(stream->input);
  69. vfree(stream->output);
  70. }
  71. kfree(stream);
  72. }
  73. static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,
  74. struct buffer_head **bh, int b, int offset, int length,
  75. struct squashfs_page_actor *output)
  76. {
  77. int res;
  78. struct squashfs_lz4 *stream = strm;
  79. squashfs_bh_to_buf(bh, b, stream->input, offset, length,
  80. msblk->devblksize);
  81. res = LZ4_decompress_safe(stream->input, stream->output,
  82. length, output->length);
  83. if (res < 0)
  84. return -EIO;
  85. squashfs_buf_to_actor(stream->output, output, res);
  86. return res;
  87. }
  88. const struct squashfs_decompressor squashfs_lz4_comp_ops = {
  89. .init = lz4_init,
  90. .comp_opts = lz4_comp_opts,
  91. .free = lz4_free,
  92. .decompress = lz4_uncompress,
  93. .id = LZ4_COMPRESSION,
  94. .name = "lz4",
  95. .supported = 1
  96. };