xz_wrapper.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, 2010
  6. * Phillip Lougher <[email protected]>
  7. *
  8. * xz_wrapper.c
  9. */
  10. #include <linux/mutex.h>
  11. #include <linux/bio.h>
  12. #include <linux/slab.h>
  13. #include <linux/xz.h>
  14. #include <linux/bitops.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 squashfs_xz {
  21. struct xz_dec *state;
  22. struct xz_buf buf;
  23. };
  24. struct disk_comp_opts {
  25. __le32 dictionary_size;
  26. __le32 flags;
  27. };
  28. struct comp_opts {
  29. int dict_size;
  30. };
  31. static void *squashfs_xz_comp_opts(struct squashfs_sb_info *msblk,
  32. void *buff, int len)
  33. {
  34. struct disk_comp_opts *comp_opts = buff;
  35. struct comp_opts *opts;
  36. int err = 0, n;
  37. opts = kmalloc(sizeof(*opts), GFP_KERNEL);
  38. if (opts == NULL) {
  39. err = -ENOMEM;
  40. goto out2;
  41. }
  42. if (comp_opts) {
  43. /* check compressor options are the expected length */
  44. if (len < sizeof(*comp_opts)) {
  45. err = -EIO;
  46. goto out;
  47. }
  48. opts->dict_size = le32_to_cpu(comp_opts->dictionary_size);
  49. /* the dictionary size should be 2^n or 2^n+2^(n+1) */
  50. n = ffs(opts->dict_size) - 1;
  51. if (opts->dict_size != (1 << n) && opts->dict_size != (1 << n) +
  52. (1 << (n + 1))) {
  53. err = -EIO;
  54. goto out;
  55. }
  56. } else
  57. /* use defaults */
  58. opts->dict_size = max_t(int, msblk->block_size,
  59. SQUASHFS_METADATA_SIZE);
  60. return opts;
  61. out:
  62. kfree(opts);
  63. out2:
  64. return ERR_PTR(err);
  65. }
  66. static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff)
  67. {
  68. struct comp_opts *comp_opts = buff;
  69. struct squashfs_xz *stream;
  70. int err;
  71. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  72. if (stream == NULL) {
  73. err = -ENOMEM;
  74. goto failed;
  75. }
  76. stream->state = xz_dec_init(XZ_PREALLOC, comp_opts->dict_size);
  77. if (stream->state == NULL) {
  78. kfree(stream);
  79. err = -ENOMEM;
  80. goto failed;
  81. }
  82. return stream;
  83. failed:
  84. ERROR("Failed to initialise xz decompressor\n");
  85. return ERR_PTR(err);
  86. }
  87. static void squashfs_xz_free(void *strm)
  88. {
  89. struct squashfs_xz *stream = strm;
  90. if (stream) {
  91. xz_dec_end(stream->state);
  92. kfree(stream);
  93. }
  94. }
  95. static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
  96. struct bio *bio, int offset, int length,
  97. struct squashfs_page_actor *output)
  98. {
  99. struct bvec_iter_all iter_all = {};
  100. struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
  101. int total = 0, error = 0;
  102. struct squashfs_xz *stream = strm;
  103. xz_dec_reset(stream->state);
  104. stream->buf.in_pos = 0;
  105. stream->buf.in_size = 0;
  106. stream->buf.out_pos = 0;
  107. stream->buf.out_size = PAGE_SIZE;
  108. stream->buf.out = squashfs_first_page(output);
  109. if (IS_ERR(stream->buf.out)) {
  110. error = PTR_ERR(stream->buf.out);
  111. goto finish;
  112. }
  113. for (;;) {
  114. enum xz_ret xz_err;
  115. if (stream->buf.in_pos == stream->buf.in_size) {
  116. const void *data;
  117. int avail;
  118. if (!bio_next_segment(bio, &iter_all)) {
  119. /* XZ_STREAM_END must be reached. */
  120. error = -EIO;
  121. break;
  122. }
  123. avail = min(length, ((int)bvec->bv_len) - offset);
  124. data = bvec_virt(bvec);
  125. length -= avail;
  126. stream->buf.in = data + offset;
  127. stream->buf.in_size = avail;
  128. stream->buf.in_pos = 0;
  129. offset = 0;
  130. }
  131. if (stream->buf.out_pos == stream->buf.out_size) {
  132. stream->buf.out = squashfs_next_page(output);
  133. if (IS_ERR(stream->buf.out)) {
  134. error = PTR_ERR(stream->buf.out);
  135. break;
  136. } else if (stream->buf.out != NULL) {
  137. stream->buf.out_pos = 0;
  138. total += PAGE_SIZE;
  139. }
  140. }
  141. xz_err = xz_dec_run(stream->state, &stream->buf);
  142. if (xz_err == XZ_STREAM_END)
  143. break;
  144. if (xz_err != XZ_OK) {
  145. error = -EIO;
  146. break;
  147. }
  148. }
  149. finish:
  150. squashfs_finish_page(output);
  151. return error ? error : total + stream->buf.out_pos;
  152. }
  153. const struct squashfs_decompressor squashfs_xz_comp_ops = {
  154. .init = squashfs_xz_init,
  155. .comp_opts = squashfs_xz_comp_opts,
  156. .free = squashfs_xz_free,
  157. .decompress = squashfs_xz_uncompress,
  158. .id = XZ_COMPRESSION,
  159. .name = "xz",
  160. .alloc_buffer = 1,
  161. .supported = 1
  162. };