zstd_wrapper.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2016-present, Facebook, Inc.
  6. * All rights reserved.
  7. *
  8. * zstd_wrapper.c
  9. */
  10. #include <linux/mutex.h>
  11. #include <linux/bio.h>
  12. #include <linux/slab.h>
  13. #include <linux/zstd.h>
  14. #include <linux/vmalloc.h>
  15. #include "squashfs_fs.h"
  16. #include "squashfs_fs_sb.h"
  17. #include "squashfs.h"
  18. #include "decompressor.h"
  19. #include "page_actor.h"
  20. struct workspace {
  21. void *mem;
  22. size_t mem_size;
  23. size_t window_size;
  24. };
  25. static void *zstd_init(struct squashfs_sb_info *msblk, void *buff)
  26. {
  27. struct workspace *wksp = kmalloc(sizeof(*wksp), GFP_KERNEL);
  28. if (wksp == NULL)
  29. goto failed;
  30. wksp->window_size = max_t(size_t,
  31. msblk->block_size, SQUASHFS_METADATA_SIZE);
  32. wksp->mem_size = zstd_dstream_workspace_bound(wksp->window_size);
  33. wksp->mem = vmalloc(wksp->mem_size);
  34. if (wksp->mem == NULL)
  35. goto failed;
  36. return wksp;
  37. failed:
  38. ERROR("Failed to allocate zstd workspace\n");
  39. kfree(wksp);
  40. return ERR_PTR(-ENOMEM);
  41. }
  42. static void zstd_free(void *strm)
  43. {
  44. struct workspace *wksp = strm;
  45. if (wksp)
  46. vfree(wksp->mem);
  47. kfree(wksp);
  48. }
  49. static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
  50. struct bio *bio, int offset, int length,
  51. struct squashfs_page_actor *output)
  52. {
  53. struct workspace *wksp = strm;
  54. zstd_dstream *stream;
  55. size_t total_out = 0;
  56. int error = 0;
  57. zstd_in_buffer in_buf = { NULL, 0, 0 };
  58. zstd_out_buffer out_buf = { NULL, 0, 0 };
  59. struct bvec_iter_all iter_all = {};
  60. struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
  61. stream = zstd_init_dstream(wksp->window_size, wksp->mem, wksp->mem_size);
  62. if (!stream) {
  63. ERROR("Failed to initialize zstd decompressor\n");
  64. return -EIO;
  65. }
  66. out_buf.size = PAGE_SIZE;
  67. out_buf.dst = squashfs_first_page(output);
  68. if (IS_ERR(out_buf.dst)) {
  69. error = PTR_ERR(out_buf.dst);
  70. goto finish;
  71. }
  72. for (;;) {
  73. size_t zstd_err;
  74. if (in_buf.pos == in_buf.size) {
  75. const void *data;
  76. int avail;
  77. if (!bio_next_segment(bio, &iter_all)) {
  78. error = -EIO;
  79. break;
  80. }
  81. avail = min(length, ((int)bvec->bv_len) - offset);
  82. data = bvec_virt(bvec);
  83. length -= avail;
  84. in_buf.src = data + offset;
  85. in_buf.size = avail;
  86. in_buf.pos = 0;
  87. offset = 0;
  88. }
  89. if (out_buf.pos == out_buf.size) {
  90. out_buf.dst = squashfs_next_page(output);
  91. if (IS_ERR(out_buf.dst)) {
  92. error = PTR_ERR(out_buf.dst);
  93. break;
  94. } else if (out_buf.dst == NULL) {
  95. /* Shouldn't run out of pages
  96. * before stream is done.
  97. */
  98. error = -EIO;
  99. break;
  100. }
  101. out_buf.pos = 0;
  102. out_buf.size = PAGE_SIZE;
  103. }
  104. total_out -= out_buf.pos;
  105. zstd_err = zstd_decompress_stream(stream, &out_buf, &in_buf);
  106. total_out += out_buf.pos; /* add the additional data produced */
  107. if (zstd_err == 0)
  108. break;
  109. if (zstd_is_error(zstd_err)) {
  110. ERROR("zstd decompression error: %d\n",
  111. (int)zstd_get_error_code(zstd_err));
  112. error = -EIO;
  113. break;
  114. }
  115. }
  116. finish:
  117. squashfs_finish_page(output);
  118. return error ? error : total_out;
  119. }
  120. const struct squashfs_decompressor squashfs_zstd_comp_ops = {
  121. .init = zstd_init,
  122. .free = zstd_free,
  123. .decompress = zstd_uncompress,
  124. .id = ZSTD_COMPRESSION,
  125. .name = "zstd",
  126. .alloc_buffer = 1,
  127. .supported = 1
  128. };