decompressor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  6. * Phillip Lougher <phillip@squashfs.org.uk>
  7. *
  8. * decompressor.c
  9. */
  10. #include <linux/types.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include "squashfs_fs.h"
  14. #include "squashfs_fs_sb.h"
  15. #include "decompressor.h"
  16. #include "squashfs.h"
  17. #include "page_actor.h"
  18. /*
  19. * This file (and decompressor.h) implements a decompressor framework for
  20. * Squashfs, allowing multiple decompressors to be easily supported
  21. */
  22. static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = {
  23. NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
  24. };
  25. #ifndef CONFIG_SQUASHFS_LZ4
  26. static const struct squashfs_decompressor squashfs_lz4_comp_ops = {
  27. NULL, NULL, NULL, NULL, LZ4_COMPRESSION, "lz4", 0
  28. };
  29. #endif
  30. #ifndef CONFIG_SQUASHFS_LZO
  31. static const struct squashfs_decompressor squashfs_lzo_comp_ops = {
  32. NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
  33. };
  34. #endif
  35. #ifndef CONFIG_SQUASHFS_XZ
  36. static const struct squashfs_decompressor squashfs_xz_comp_ops = {
  37. NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
  38. };
  39. #endif
  40. #ifndef CONFIG_SQUASHFS_ZLIB
  41. static const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  42. NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
  43. };
  44. #endif
  45. #ifndef CONFIG_SQUASHFS_ZSTD
  46. static const struct squashfs_decompressor squashfs_zstd_comp_ops = {
  47. NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0
  48. };
  49. #endif
  50. static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
  51. NULL, NULL, NULL, NULL, 0, "unknown", 0
  52. };
  53. static const struct squashfs_decompressor *decompressor[] = {
  54. &squashfs_zlib_comp_ops,
  55. &squashfs_lz4_comp_ops,
  56. &squashfs_lzo_comp_ops,
  57. &squashfs_xz_comp_ops,
  58. &squashfs_lzma_unsupported_comp_ops,
  59. &squashfs_zstd_comp_ops,
  60. &squashfs_unknown_comp_ops
  61. };
  62. const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
  63. {
  64. int i;
  65. for (i = 0; decompressor[i]->id; i++)
  66. if (id == decompressor[i]->id)
  67. break;
  68. return decompressor[i];
  69. }
  70. static void *get_comp_opts(struct super_block *sb, unsigned short flags)
  71. {
  72. struct squashfs_sb_info *msblk = sb->s_fs_info;
  73. void *buffer = NULL, *comp_opts;
  74. struct squashfs_page_actor *actor = NULL;
  75. int length = 0;
  76. /*
  77. * Read decompressor specific options from file system if present
  78. */
  79. if (SQUASHFS_COMP_OPTS(flags)) {
  80. buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  81. if (buffer == NULL) {
  82. comp_opts = ERR_PTR(-ENOMEM);
  83. goto out;
  84. }
  85. actor = squashfs_page_actor_init(&buffer, 1, 0);
  86. if (actor == NULL) {
  87. comp_opts = ERR_PTR(-ENOMEM);
  88. goto out;
  89. }
  90. length = squashfs_read_data(sb,
  91. sizeof(struct squashfs_super_block), 0, NULL, actor);
  92. if (length < 0) {
  93. comp_opts = ERR_PTR(length);
  94. goto out;
  95. }
  96. }
  97. comp_opts = squashfs_comp_opts(msblk, buffer, length);
  98. out:
  99. kfree(actor);
  100. kfree(buffer);
  101. return comp_opts;
  102. }
  103. void *squashfs_decompressor_setup(struct super_block *sb, unsigned short flags)
  104. {
  105. struct squashfs_sb_info *msblk = sb->s_fs_info;
  106. void *stream, *comp_opts = get_comp_opts(sb, flags);
  107. if (IS_ERR(comp_opts))
  108. return comp_opts;
  109. stream = msblk->thread_ops->create(msblk, comp_opts);
  110. if (IS_ERR(stream))
  111. kfree(comp_opts);
  112. return stream;
  113. }