xz_wrapper.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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@lougher.demon.co.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 "squashfs_fs.h"
  28. #include "squashfs_fs_sb.h"
  29. #include "squashfs_fs_i.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. static void *squashfs_xz_init(struct squashfs_sb_info *msblk)
  37. {
  38. int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
  39. struct squashfs_xz *stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  40. if (stream == NULL)
  41. goto failed;
  42. stream->state = xz_dec_init(XZ_PREALLOC, block_size);
  43. if (stream->state == NULL)
  44. goto failed;
  45. return stream;
  46. failed:
  47. ERROR("Failed to allocate xz workspace\n");
  48. kfree(stream);
  49. return NULL;
  50. }
  51. static void squashfs_xz_free(void *strm)
  52. {
  53. struct squashfs_xz *stream = strm;
  54. if (stream) {
  55. xz_dec_end(stream->state);
  56. kfree(stream);
  57. }
  58. }
  59. static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  60. struct buffer_head **bh, int b, int offset, int length, int srclength,
  61. int pages)
  62. {
  63. enum xz_ret xz_err;
  64. int avail, total = 0, k = 0, page = 0;
  65. struct squashfs_xz *stream = msblk->stream;
  66. mutex_lock(&msblk->read_data_mutex);
  67. xz_dec_reset(stream->state);
  68. stream->buf.in_pos = 0;
  69. stream->buf.in_size = 0;
  70. stream->buf.out_pos = 0;
  71. stream->buf.out_size = PAGE_CACHE_SIZE;
  72. stream->buf.out = buffer[page++];
  73. do {
  74. if (stream->buf.in_pos == stream->buf.in_size && k < b) {
  75. avail = min(length, msblk->devblksize - offset);
  76. length -= avail;
  77. wait_on_buffer(bh[k]);
  78. if (!buffer_uptodate(bh[k]))
  79. goto release_mutex;
  80. stream->buf.in = bh[k]->b_data + offset;
  81. stream->buf.in_size = avail;
  82. stream->buf.in_pos = 0;
  83. offset = 0;
  84. }
  85. if (stream->buf.out_pos == stream->buf.out_size
  86. && page < pages) {
  87. stream->buf.out = buffer[page++];
  88. stream->buf.out_pos = 0;
  89. total += PAGE_CACHE_SIZE;
  90. }
  91. xz_err = xz_dec_run(stream->state, &stream->buf);
  92. if (stream->buf.in_pos == stream->buf.in_size && k < b)
  93. put_bh(bh[k++]);
  94. } while (xz_err == XZ_OK);
  95. if (xz_err != XZ_STREAM_END) {
  96. ERROR("xz_dec_run error, data probably corrupt\n");
  97. goto release_mutex;
  98. }
  99. if (k < b) {
  100. ERROR("xz_uncompress error, input remaining\n");
  101. goto release_mutex;
  102. }
  103. total += stream->buf.out_pos;
  104. mutex_unlock(&msblk->read_data_mutex);
  105. return total;
  106. release_mutex:
  107. mutex_unlock(&msblk->read_data_mutex);
  108. for (; k < b; k++)
  109. put_bh(bh[k]);
  110. return -EIO;
  111. }
  112. const struct squashfs_decompressor squashfs_xz_comp_ops = {
  113. .init = squashfs_xz_init,
  114. .free = squashfs_xz_free,
  115. .decompress = squashfs_xz_uncompress,
  116. .id = XZ_COMPRESSION,
  117. .name = "xz",
  118. .supported = 1
  119. };