Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Squashfs - a compressed read only filesystem for Linux |
| 3 | * |
| 4 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 5 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 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 | |
| 24 | |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/buffer_head.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/xz.h> |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 29 | #include <linux/bitops.h> |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 30 | |
| 31 | #include "squashfs_fs.h" |
| 32 | #include "squashfs_fs_sb.h" |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 33 | #include "squashfs.h" |
| 34 | #include "decompressor.h" |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 35 | #include "page_actor.h" |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 36 | |
| 37 | struct squashfs_xz { |
| 38 | struct xz_dec *state; |
| 39 | struct xz_buf buf; |
| 40 | }; |
| 41 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 42 | struct disk_comp_opts { |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 43 | __le32 dictionary_size; |
| 44 | __le32 flags; |
| 45 | }; |
| 46 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 47 | struct comp_opts { |
| 48 | int dict_size; |
| 49 | }; |
| 50 | |
| 51 | static void *squashfs_xz_comp_opts(struct squashfs_sb_info *msblk, |
| 52 | void *buff, int len) |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 53 | { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 54 | struct disk_comp_opts *comp_opts = buff; |
| 55 | struct comp_opts *opts; |
| 56 | int err = 0, n; |
| 57 | |
| 58 | opts = kmalloc(sizeof(*opts), GFP_KERNEL); |
| 59 | if (opts == NULL) { |
| 60 | err = -ENOMEM; |
| 61 | goto out2; |
| 62 | } |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 63 | |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 64 | if (comp_opts) { |
| 65 | /* check compressor options are the expected length */ |
| 66 | if (len < sizeof(*comp_opts)) { |
| 67 | err = -EIO; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 68 | goto out; |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 69 | } |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 70 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 71 | opts->dict_size = le32_to_cpu(comp_opts->dictionary_size); |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 72 | |
| 73 | /* the dictionary size should be 2^n or 2^n+2^(n+1) */ |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 74 | n = ffs(opts->dict_size) - 1; |
| 75 | if (opts->dict_size != (1 << n) && opts->dict_size != (1 << n) + |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 76 | (1 << (n + 1))) { |
| 77 | err = -EIO; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 78 | goto out; |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 79 | } |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 80 | } else |
| 81 | /* use defaults */ |
| 82 | opts->dict_size = max_t(int, msblk->block_size, |
| 83 | SQUASHFS_METADATA_SIZE); |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 84 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 85 | return opts; |
| 86 | |
| 87 | out: |
| 88 | kfree(opts); |
| 89 | out2: |
| 90 | return ERR_PTR(err); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff) |
| 95 | { |
| 96 | struct comp_opts *comp_opts = buff; |
| 97 | struct squashfs_xz *stream; |
| 98 | int err; |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 99 | |
| 100 | stream = kmalloc(sizeof(*stream), GFP_KERNEL); |
| 101 | if (stream == NULL) { |
| 102 | err = -ENOMEM; |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 103 | goto failed; |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 106 | stream->state = xz_dec_init(XZ_PREALLOC, comp_opts->dict_size); |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 107 | if (stream->state == NULL) { |
| 108 | kfree(stream); |
| 109 | err = -ENOMEM; |
| 110 | goto failed; |
| 111 | } |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 112 | |
| 113 | return stream; |
| 114 | |
| 115 | failed: |
Phillip Lougher | ff75031 | 2011-02-28 15:31:46 +0000 | [diff] [blame] | 116 | ERROR("Failed to initialise xz decompressor\n"); |
| 117 | return ERR_PTR(err); |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | static void squashfs_xz_free(void *strm) |
| 122 | { |
| 123 | struct squashfs_xz *stream = strm; |
| 124 | |
| 125 | if (stream) { |
| 126 | xz_dec_end(stream->state); |
| 127 | kfree(stream); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 132 | static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 133 | struct buffer_head **bh, int b, int offset, int length, |
| 134 | struct squashfs_page_actor *output) |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 135 | { |
| 136 | enum xz_ret xz_err; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 137 | int avail, total = 0, k = 0; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 138 | struct squashfs_xz *stream = strm; |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 139 | |
| 140 | xz_dec_reset(stream->state); |
| 141 | stream->buf.in_pos = 0; |
| 142 | stream->buf.in_size = 0; |
| 143 | stream->buf.out_pos = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame^] | 144 | stream->buf.out_size = PAGE_SIZE; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 145 | stream->buf.out = squashfs_first_page(output); |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 146 | |
| 147 | do { |
| 148 | if (stream->buf.in_pos == stream->buf.in_size && k < b) { |
| 149 | avail = min(length, msblk->devblksize - offset); |
| 150 | length -= avail; |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 151 | stream->buf.in = bh[k]->b_data + offset; |
| 152 | stream->buf.in_size = avail; |
| 153 | stream->buf.in_pos = 0; |
| 154 | offset = 0; |
| 155 | } |
| 156 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 157 | if (stream->buf.out_pos == stream->buf.out_size) { |
| 158 | stream->buf.out = squashfs_next_page(output); |
| 159 | if (stream->buf.out != NULL) { |
| 160 | stream->buf.out_pos = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame^] | 161 | total += PAGE_SIZE; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 162 | } |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | xz_err = xz_dec_run(stream->state, &stream->buf); |
| 166 | |
| 167 | if (stream->buf.in_pos == stream->buf.in_size && k < b) |
| 168 | put_bh(bh[k++]); |
| 169 | } while (xz_err == XZ_OK); |
| 170 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 171 | squashfs_finish_page(output); |
| 172 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 173 | if (xz_err != XZ_STREAM_END || k < b) |
| 174 | goto out; |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 175 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 176 | return total + stream->buf.out_pos; |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 177 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 178 | out: |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 179 | for (; k < b; k++) |
| 180 | put_bh(bh[k]); |
| 181 | |
| 182 | return -EIO; |
| 183 | } |
| 184 | |
| 185 | const struct squashfs_decompressor squashfs_xz_comp_ops = { |
| 186 | .init = squashfs_xz_init, |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 187 | .comp_opts = squashfs_xz_comp_opts, |
Phillip Lougher | 81bb8de | 2010-12-09 02:02:29 +0000 | [diff] [blame] | 188 | .free = squashfs_xz_free, |
| 189 | .decompress = squashfs_xz_uncompress, |
| 190 | .id = XZ_COMPRESSION, |
| 191 | .name = "xz", |
| 192 | .supported = 1 |
| 193 | }; |