lz4_wrapper.c 3.0 KB

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