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