decompressor_single.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013
  4. * Phillip Lougher <[email protected]>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/mutex.h>
  8. #include <linux/slab.h>
  9. #include <linux/bio.h>
  10. #include "squashfs_fs.h"
  11. #include "squashfs_fs_sb.h"
  12. #include "decompressor.h"
  13. #include "squashfs.h"
  14. /*
  15. * This file implements single-threaded decompression in the
  16. * decompressor framework
  17. */
  18. struct squashfs_stream {
  19. void *stream;
  20. struct mutex mutex;
  21. };
  22. void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
  23. void *comp_opts)
  24. {
  25. struct squashfs_stream *stream;
  26. int err = -ENOMEM;
  27. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  28. if (stream == NULL)
  29. goto out;
  30. stream->stream = msblk->decompressor->init(msblk, comp_opts);
  31. if (IS_ERR(stream->stream)) {
  32. err = PTR_ERR(stream->stream);
  33. goto out;
  34. }
  35. kfree(comp_opts);
  36. mutex_init(&stream->mutex);
  37. return stream;
  38. out:
  39. kfree(stream);
  40. return ERR_PTR(err);
  41. }
  42. void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
  43. {
  44. struct squashfs_stream *stream = msblk->stream;
  45. if (stream) {
  46. msblk->decompressor->free(stream->stream);
  47. kfree(stream);
  48. }
  49. }
  50. int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
  51. int offset, int length,
  52. struct squashfs_page_actor *output)
  53. {
  54. int res;
  55. struct squashfs_stream *stream = msblk->stream;
  56. mutex_lock(&stream->mutex);
  57. res = msblk->decompressor->decompress(msblk, stream->stream, bio,
  58. offset, length, output);
  59. mutex_unlock(&stream->mutex);
  60. if (res < 0)
  61. ERROR("%s decompression failed, data probably corrupt\n",
  62. msblk->decompressor->name);
  63. return res;
  64. }
  65. int squashfs_max_decompressors(void)
  66. {
  67. return 1;
  68. }