xz_wrapper.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
  5. * Phillip Lougher <phillip@squashfs.org.uk>
  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. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * xz_wrapper.c
  22. */
  23. #include <linux/mutex.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/slab.h>
  26. #include <linux/xz.h>
  27. #include <linux/bitops.h>
  28. #include "squashfs_fs.h"
  29. #include "squashfs_fs_sb.h"
  30. #include "squashfs.h"
  31. #include "decompressor.h"
  32. #include "page_actor.h"
  33. struct squashfs_xz {
  34. struct xz_dec *state;
  35. struct xz_buf buf;
  36. };
  37. struct disk_comp_opts {
  38. __le32 dictionary_size;
  39. __le32 flags;
  40. };
  41. struct comp_opts {
  42. int dict_size;
  43. };
  44. static void *squashfs_xz_comp_opts(struct squashfs_sb_info *msblk,
  45. void *buff, int len)
  46. {
  47. struct disk_comp_opts *comp_opts = buff;
  48. struct comp_opts *opts;
  49. int err = 0, n;
  50. opts = kmalloc(sizeof(*opts), GFP_KERNEL);
  51. if (opts == NULL) {
  52. err = -ENOMEM;
  53. goto out2;
  54. }
  55. if (comp_opts) {
  56. /* check compressor options are the expected length */
  57. if (len < sizeof(*comp_opts)) {
  58. err = -EIO;
  59. goto out;
  60. }
  61. opts->dict_size = le32_to_cpu(comp_opts->dictionary_size);
  62. /* the dictionary size should be 2^n or 2^n+2^(n+1) */
  63. n = ffs(opts->dict_size) - 1;
  64. if (opts->dict_size != (1 << n) && opts->dict_size != (1 << n) +
  65. (1 << (n + 1))) {
  66. err = -EIO;
  67. goto out;
  68. }
  69. } else
  70. /* use defaults */
  71. opts->dict_size = max_t(int, msblk->block_size,
  72. SQUASHFS_METADATA_SIZE);
  73. return opts;
  74. out:
  75. kfree(opts);
  76. out2:
  77. return ERR_PTR(err);
  78. }
  79. static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff)
  80. {
  81. struct comp_opts *comp_opts = buff;
  82. struct squashfs_xz *stream;
  83. int err;
  84. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  85. if (stream == NULL) {
  86. err = -ENOMEM;
  87. goto failed;
  88. }
  89. stream->state = xz_dec_init(XZ_PREALLOC, comp_opts->dict_size);
  90. if (stream->state == NULL) {
  91. kfree(stream);
  92. err = -ENOMEM;
  93. goto failed;
  94. }
  95. return stream;
  96. failed:
  97. ERROR("Failed to initialise xz decompressor\n");
  98. return ERR_PTR(err);
  99. }
  100. static void squashfs_xz_free(void *strm)
  101. {
  102. struct squashfs_xz *stream = strm;
  103. if (stream) {
  104. xz_dec_end(stream->state);
  105. kfree(stream);
  106. }
  107. }
  108. static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
  109. struct buffer_head **bh, int b, int offset, int length,
  110. struct squashfs_page_actor *output)
  111. {
  112. enum xz_ret xz_err;
  113. int avail, total = 0, k = 0;
  114. struct squashfs_xz *stream = strm;
  115. xz_dec_reset(stream->state);
  116. stream->buf.in_pos = 0;
  117. stream->buf.in_size = 0;
  118. stream->buf.out_pos = 0;
  119. stream->buf.out_size = PAGE_CACHE_SIZE;
  120. stream->buf.out = squashfs_first_page(output);
  121. do {
  122. if (stream->buf.in_pos == stream->buf.in_size && k < b) {
  123. avail = min(length, msblk->devblksize - offset);
  124. length -= avail;
  125. stream->buf.in = bh[k]->b_data + offset;
  126. stream->buf.in_size = avail;
  127. stream->buf.in_pos = 0;
  128. offset = 0;
  129. }
  130. if (stream->buf.out_pos == stream->buf.out_size) {
  131. stream->buf.out = squashfs_next_page(output);
  132. if (stream->buf.out != NULL) {
  133. stream->buf.out_pos = 0;
  134. total += PAGE_CACHE_SIZE;
  135. }
  136. }
  137. xz_err = xz_dec_run(stream->state, &stream->buf);
  138. if (stream->buf.in_pos == stream->buf.in_size && k < b)
  139. put_bh(bh[k++]);
  140. } while (xz_err == XZ_OK);
  141. squashfs_finish_page(output);
  142. if (xz_err != XZ_STREAM_END || k < b)
  143. goto out;
  144. return total + stream->buf.out_pos;
  145. out:
  146. for (; k < b; k++)
  147. put_bh(bh[k]);
  148. return -EIO;
  149. }
  150. const struct squashfs_decompressor squashfs_xz_comp_ops = {
  151. .init = squashfs_xz_init,
  152. .comp_opts = squashfs_xz_comp_opts,
  153. .free = squashfs_xz_free,
  154. .decompress = squashfs_xz_uncompress,
  155. .id = XZ_COMPRESSION,
  156. .name = "xz",
  157. .supported = 1
  158. };