lz4_wrapper.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. struct squashfs_lz4 *stream = strm;
  78. void *buff = stream->input, *data;
  79. int avail, i, bytes = length, res;
  80. for (i = 0; i < b; i++) {
  81. avail = min(bytes, msblk->devblksize - offset);
  82. memcpy(buff, bh[i]->b_data + offset, avail);
  83. buff += avail;
  84. bytes -= avail;
  85. offset = 0;
  86. put_bh(bh[i]);
  87. }
  88. res = LZ4_decompress_safe(stream->input, stream->output,
  89. length, output->length);
  90. if (res < 0)
  91. return -EIO;
  92. bytes = res;
  93. data = squashfs_first_page(output);
  94. buff = stream->output;
  95. while (data) {
  96. if (bytes <= PAGE_SIZE) {
  97. memcpy(data, buff, bytes);
  98. break;
  99. }
  100. memcpy(data, buff, PAGE_SIZE);
  101. buff += PAGE_SIZE;
  102. bytes -= PAGE_SIZE;
  103. data = squashfs_next_page(output);
  104. }
  105. squashfs_finish_page(output);
  106. return res;
  107. }
  108. const struct squashfs_decompressor squashfs_lz4_comp_ops = {
  109. .init = lz4_init,
  110. .comp_opts = lz4_comp_opts,
  111. .free = lz4_free,
  112. .decompress = lz4_uncompress,
  113. .id = LZ4_COMPRESSION,
  114. .name = "lz4",
  115. .supported = 1
  116. };