xz_wrapper.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. struct squashfs_xz {
  33. struct xz_dec *state;
  34. struct xz_buf buf;
  35. };
  36. struct comp_opts {
  37. __le32 dictionary_size;
  38. __le32 flags;
  39. };
  40. static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff,
  41. int len)
  42. {
  43. struct comp_opts *comp_opts = buff;
  44. struct squashfs_xz *stream;
  45. int dict_size = msblk->block_size;
  46. int err, n;
  47. if (comp_opts) {
  48. /* check compressor options are the expected length */
  49. if (len < sizeof(*comp_opts)) {
  50. err = -EIO;
  51. goto failed;
  52. }
  53. dict_size = le32_to_cpu(comp_opts->dictionary_size);
  54. /* the dictionary size should be 2^n or 2^n+2^(n+1) */
  55. n = ffs(dict_size) - 1;
  56. if (dict_size != (1 << n) && dict_size != (1 << n) +
  57. (1 << (n + 1))) {
  58. err = -EIO;
  59. goto failed;
  60. }
  61. }
  62. dict_size = max_t(int, dict_size, SQUASHFS_METADATA_SIZE);
  63. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  64. if (stream == NULL) {
  65. err = -ENOMEM;
  66. goto failed;
  67. }
  68. stream->state = xz_dec_init(XZ_PREALLOC, dict_size);
  69. if (stream->state == NULL) {
  70. kfree(stream);
  71. err = -ENOMEM;
  72. goto failed;
  73. }
  74. return stream;
  75. failed:
  76. ERROR("Failed to initialise xz decompressor\n");
  77. return ERR_PTR(err);
  78. }
  79. static void squashfs_xz_free(void *strm)
  80. {
  81. struct squashfs_xz *stream = strm;
  82. if (stream) {
  83. xz_dec_end(stream->state);
  84. kfree(stream);
  85. }
  86. }
  87. static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  88. struct buffer_head **bh, int b, int offset, int length, int srclength,
  89. int pages)
  90. {
  91. enum xz_ret xz_err;
  92. int avail, total = 0, k = 0, page = 0;
  93. struct squashfs_xz *stream = msblk->stream;
  94. mutex_lock(&msblk->read_data_mutex);
  95. xz_dec_reset(stream->state);
  96. stream->buf.in_pos = 0;
  97. stream->buf.in_size = 0;
  98. stream->buf.out_pos = 0;
  99. stream->buf.out_size = PAGE_CACHE_SIZE;
  100. stream->buf.out = buffer[page++];
  101. do {
  102. if (stream->buf.in_pos == stream->buf.in_size && k < b) {
  103. avail = min(length, msblk->devblksize - offset);
  104. length -= avail;
  105. wait_on_buffer(bh[k]);
  106. if (!buffer_uptodate(bh[k]))
  107. goto release_mutex;
  108. stream->buf.in = bh[k]->b_data + offset;
  109. stream->buf.in_size = avail;
  110. stream->buf.in_pos = 0;
  111. offset = 0;
  112. }
  113. if (stream->buf.out_pos == stream->buf.out_size
  114. && page < pages) {
  115. stream->buf.out = buffer[page++];
  116. stream->buf.out_pos = 0;
  117. total += PAGE_CACHE_SIZE;
  118. }
  119. xz_err = xz_dec_run(stream->state, &stream->buf);
  120. if (stream->buf.in_pos == stream->buf.in_size && k < b)
  121. put_bh(bh[k++]);
  122. } while (xz_err == XZ_OK);
  123. if (xz_err != XZ_STREAM_END) {
  124. ERROR("xz_dec_run error, data probably corrupt\n");
  125. goto release_mutex;
  126. }
  127. if (k < b) {
  128. ERROR("xz_uncompress error, input remaining\n");
  129. goto release_mutex;
  130. }
  131. total += stream->buf.out_pos;
  132. mutex_unlock(&msblk->read_data_mutex);
  133. return total;
  134. release_mutex:
  135. mutex_unlock(&msblk->read_data_mutex);
  136. for (; k < b; k++)
  137. put_bh(bh[k]);
  138. return -EIO;
  139. }
  140. const struct squashfs_decompressor squashfs_xz_comp_ops = {
  141. .init = squashfs_xz_init,
  142. .free = squashfs_xz_free,
  143. .decompress = squashfs_xz_uncompress,
  144. .id = XZ_COMPRESSION,
  145. .name = "xz",
  146. .supported = 1
  147. };