zstd_wrapper.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2016-present, Facebook, Inc.
  5. * All rights reserved.
  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. * zstd_wrapper.c
  18. */
  19. #include <linux/mutex.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/slab.h>
  22. #include <linux/zstd.h>
  23. #include <linux/vmalloc.h>
  24. #include "squashfs_fs.h"
  25. #include "squashfs_fs_sb.h"
  26. #include "squashfs.h"
  27. #include "decompressor.h"
  28. #include "page_actor.h"
  29. struct workspace {
  30. void *mem;
  31. size_t mem_size;
  32. size_t window_size;
  33. };
  34. static void *zstd_init(struct squashfs_sb_info *msblk, void *buff)
  35. {
  36. struct workspace *wksp = kmalloc(sizeof(*wksp), GFP_KERNEL);
  37. if (wksp == NULL)
  38. goto failed;
  39. wksp->window_size = max_t(size_t,
  40. msblk->block_size, SQUASHFS_METADATA_SIZE);
  41. wksp->mem_size = ZSTD_DStreamWorkspaceBound(wksp->window_size);
  42. wksp->mem = vmalloc(wksp->mem_size);
  43. if (wksp->mem == NULL)
  44. goto failed;
  45. return wksp;
  46. failed:
  47. ERROR("Failed to allocate zstd workspace\n");
  48. kfree(wksp);
  49. return ERR_PTR(-ENOMEM);
  50. }
  51. static void zstd_free(void *strm)
  52. {
  53. struct workspace *wksp = strm;
  54. if (wksp)
  55. vfree(wksp->mem);
  56. kfree(wksp);
  57. }
  58. static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
  59. struct buffer_head **bh, int b, int offset, int length,
  60. struct squashfs_page_actor *output)
  61. {
  62. struct workspace *wksp = strm;
  63. ZSTD_DStream *stream;
  64. size_t total_out = 0;
  65. size_t zstd_err;
  66. int k = 0;
  67. ZSTD_inBuffer in_buf = { NULL, 0, 0 };
  68. ZSTD_outBuffer out_buf = { NULL, 0, 0 };
  69. stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size);
  70. if (!stream) {
  71. ERROR("Failed to initialize zstd decompressor\n");
  72. goto out;
  73. }
  74. out_buf.size = PAGE_SIZE;
  75. out_buf.dst = squashfs_first_page(output);
  76. do {
  77. if (in_buf.pos == in_buf.size && k < b) {
  78. int avail = min(length, msblk->devblksize - offset);
  79. length -= avail;
  80. in_buf.src = bh[k]->b_data + offset;
  81. in_buf.size = avail;
  82. in_buf.pos = 0;
  83. offset = 0;
  84. }
  85. if (out_buf.pos == out_buf.size) {
  86. out_buf.dst = squashfs_next_page(output);
  87. if (out_buf.dst == NULL) {
  88. /* Shouldn't run out of pages
  89. * before stream is done.
  90. */
  91. squashfs_finish_page(output);
  92. goto out;
  93. }
  94. out_buf.pos = 0;
  95. out_buf.size = PAGE_SIZE;
  96. }
  97. total_out -= out_buf.pos;
  98. zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf);
  99. total_out += out_buf.pos; /* add the additional data produced */
  100. if (in_buf.pos == in_buf.size && k < b)
  101. put_bh(bh[k++]);
  102. } while (zstd_err != 0 && !ZSTD_isError(zstd_err));
  103. squashfs_finish_page(output);
  104. if (ZSTD_isError(zstd_err)) {
  105. ERROR("zstd decompression error: %d\n",
  106. (int)ZSTD_getErrorCode(zstd_err));
  107. goto out;
  108. }
  109. if (k < b)
  110. goto out;
  111. return (int)total_out;
  112. out:
  113. for (; k < b; k++)
  114. put_bh(bh[k]);
  115. return -EIO;
  116. }
  117. const struct squashfs_decompressor squashfs_zstd_comp_ops = {
  118. .init = zstd_init,
  119. .free = zstd_free,
  120. .decompress = zstd_uncompress,
  121. .id = ZSTD_COMPRESSION,
  122. .name = "zstd",
  123. .supported = 1
  124. };