1 /* 2 * Squashfs - a compressed read only filesystem for Linux 3 * 4 * Copyright (c) 2010 LG Electronics 5 * Chan Jeong <chan.jeong@lge.com> 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 * lzo_wrapper.c 22 */ 23 24 #include <linux/mutex.h> 25 #include <linux/buffer_head.h> 26 #include <linux/slab.h> 27 #include <linux/vmalloc.h> 28 #include <linux/lzo.h> 29 30 #include "squashfs_fs.h" 31 #include "squashfs_fs_sb.h" 32 #include "squashfs_fs_i.h" 33 #include "squashfs.h" 34 #include "decompressor.h" 35 36 struct squashfs_lzo { 37 void *input; 38 void *output; 39 }; 40 41 static void *lzo_init(struct squashfs_sb_info *msblk) 42 { 43 struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL); 44 if (stream == NULL) 45 goto failed; 46 stream->input = vmalloc(msblk->block_size); 47 if (stream->input == NULL) 48 goto failed; 49 stream->output = vmalloc(msblk->block_size); 50 if (stream->output == NULL) 51 goto failed2; 52 53 return stream; 54 55 failed2: 56 vfree(stream->input); 57 failed: 58 ERROR("Failed to allocate lzo workspace\n"); 59 kfree(stream); 60 return NULL; 61 } 62 63 64 static void lzo_free(void *strm) 65 { 66 struct squashfs_lzo *stream = strm; 67 68 if (stream) { 69 vfree(stream->input); 70 vfree(stream->output); 71 } 72 kfree(stream); 73 } 74 75 76 static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer, 77 struct buffer_head **bh, int b, int offset, int length, int srclength, 78 int pages) 79 { 80 struct squashfs_lzo *stream = msblk->stream; 81 void *buff = stream->input; 82 int avail, i, bytes = length, res; 83 size_t out_len = msblk->block_size; 84 85 mutex_lock(&msblk->read_data_mutex); 86 87 for (i = 0; i < b; i++) { 88 wait_on_buffer(bh[i]); 89 if (!buffer_uptodate(bh[i])) 90 goto block_release; 91 92 avail = min(bytes, msblk->devblksize - offset); 93 memcpy(buff, bh[i]->b_data + offset, avail); 94 buff += avail; 95 bytes -= avail; 96 offset = 0; 97 put_bh(bh[i]); 98 } 99 100 res = lzo1x_decompress_safe(stream->input, (size_t)length, 101 stream->output, &out_len); 102 if (res != LZO_E_OK) 103 goto failed; 104 105 res = bytes = (int)out_len; 106 for (i = 0, buff = stream->output; bytes && i < pages; i++) { 107 avail = min_t(int, bytes, PAGE_CACHE_SIZE); 108 memcpy(buffer[i], buff, avail); 109 buff += avail; 110 bytes -= avail; 111 } 112 113 mutex_unlock(&msblk->read_data_mutex); 114 return res; 115 116 block_release: 117 for (; i < b; i++) 118 put_bh(bh[i]); 119 120 failed: 121 mutex_unlock(&msblk->read_data_mutex); 122 123 ERROR("lzo decompression failed, data probably corrupt\n"); 124 return -EIO; 125 } 126 127 const struct squashfs_decompressor squashfs_lzo_comp_ops = { 128 .init = lzo_init, 129 .free = lzo_free, 130 .decompress = lzo_uncompress, 131 .id = LZO_COMPRESSION, 132 .name = "lzo", 133 .supported = 1 134 }; 135