179cb8cedSChan Jeong /* 279cb8cedSChan Jeong * Squashfs - a compressed read only filesystem for Linux 379cb8cedSChan Jeong * 479cb8cedSChan Jeong * Copyright (c) 2010 LG Electronics 579cb8cedSChan Jeong * Chan Jeong <chan.jeong@lge.com> 679cb8cedSChan Jeong * 779cb8cedSChan Jeong * This program is free software; you can redistribute it and/or 879cb8cedSChan Jeong * modify it under the terms of the GNU General Public License 979cb8cedSChan Jeong * as published by the Free Software Foundation; either version 2, 1079cb8cedSChan Jeong * or (at your option) any later version. 1179cb8cedSChan Jeong * 1279cb8cedSChan Jeong * This program is distributed in the hope that it will be useful, 1379cb8cedSChan Jeong * but WITHOUT ANY WARRANTY; without even the implied warranty of 1479cb8cedSChan Jeong * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1579cb8cedSChan Jeong * GNU General Public License for more details. 1679cb8cedSChan Jeong * 1779cb8cedSChan Jeong * You should have received a copy of the GNU General Public License 1879cb8cedSChan Jeong * along with this program; if not, write to the Free Software 1979cb8cedSChan Jeong * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 2079cb8cedSChan Jeong * 2179cb8cedSChan Jeong * lzo_wrapper.c 2279cb8cedSChan Jeong */ 2379cb8cedSChan Jeong 2479cb8cedSChan Jeong #include <linux/mutex.h> 2579cb8cedSChan Jeong #include <linux/buffer_head.h> 2679cb8cedSChan Jeong #include <linux/slab.h> 2779cb8cedSChan Jeong #include <linux/vmalloc.h> 2879cb8cedSChan Jeong #include <linux/lzo.h> 2979cb8cedSChan Jeong 3079cb8cedSChan Jeong #include "squashfs_fs.h" 3179cb8cedSChan Jeong #include "squashfs_fs_sb.h" 3279cb8cedSChan Jeong #include "squashfs_fs_i.h" 3379cb8cedSChan Jeong #include "squashfs.h" 3479cb8cedSChan Jeong #include "decompressor.h" 3579cb8cedSChan Jeong 3679cb8cedSChan Jeong struct squashfs_lzo { 3779cb8cedSChan Jeong void *input; 3879cb8cedSChan Jeong void *output; 3979cb8cedSChan Jeong }; 4079cb8cedSChan Jeong 4179cb8cedSChan Jeong static void *lzo_init(struct squashfs_sb_info *msblk) 4279cb8cedSChan Jeong { 43f3065f60SPhillip Lougher int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE); 44f3065f60SPhillip Lougher 4579cb8cedSChan Jeong struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL); 4679cb8cedSChan Jeong if (stream == NULL) 4779cb8cedSChan Jeong goto failed; 48f3065f60SPhillip Lougher stream->input = vmalloc(block_size); 4979cb8cedSChan Jeong if (stream->input == NULL) 5079cb8cedSChan Jeong goto failed; 51f3065f60SPhillip Lougher stream->output = vmalloc(block_size); 5279cb8cedSChan Jeong if (stream->output == NULL) 5379cb8cedSChan Jeong goto failed2; 5479cb8cedSChan Jeong 5579cb8cedSChan Jeong return stream; 5679cb8cedSChan Jeong 5779cb8cedSChan Jeong failed2: 5879cb8cedSChan Jeong vfree(stream->input); 5979cb8cedSChan Jeong failed: 6079cb8cedSChan Jeong ERROR("Failed to allocate lzo workspace\n"); 6179cb8cedSChan Jeong kfree(stream); 6279cb8cedSChan Jeong return NULL; 6379cb8cedSChan Jeong } 6479cb8cedSChan Jeong 6579cb8cedSChan Jeong 6679cb8cedSChan Jeong static void lzo_free(void *strm) 6779cb8cedSChan Jeong { 6879cb8cedSChan Jeong struct squashfs_lzo *stream = strm; 6979cb8cedSChan Jeong 7079cb8cedSChan Jeong if (stream) { 7179cb8cedSChan Jeong vfree(stream->input); 7279cb8cedSChan Jeong vfree(stream->output); 7379cb8cedSChan Jeong } 7479cb8cedSChan Jeong kfree(stream); 7579cb8cedSChan Jeong } 7679cb8cedSChan Jeong 7779cb8cedSChan Jeong 7879cb8cedSChan Jeong static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer, 7979cb8cedSChan Jeong struct buffer_head **bh, int b, int offset, int length, int srclength, 8079cb8cedSChan Jeong int pages) 8179cb8cedSChan Jeong { 8279cb8cedSChan Jeong struct squashfs_lzo *stream = msblk->stream; 8379cb8cedSChan Jeong void *buff = stream->input; 8479cb8cedSChan Jeong int avail, i, bytes = length, res; 85f3065f60SPhillip Lougher size_t out_len = srclength; 8679cb8cedSChan Jeong 8779cb8cedSChan Jeong mutex_lock(&msblk->read_data_mutex); 8879cb8cedSChan Jeong 8979cb8cedSChan Jeong for (i = 0; i < b; i++) { 9079cb8cedSChan Jeong wait_on_buffer(bh[i]); 9179cb8cedSChan Jeong if (!buffer_uptodate(bh[i])) 9279cb8cedSChan Jeong goto block_release; 9379cb8cedSChan Jeong 9479cb8cedSChan Jeong avail = min(bytes, msblk->devblksize - offset); 9579cb8cedSChan Jeong memcpy(buff, bh[i]->b_data + offset, avail); 9679cb8cedSChan Jeong buff += avail; 9779cb8cedSChan Jeong bytes -= avail; 9879cb8cedSChan Jeong offset = 0; 9979cb8cedSChan Jeong put_bh(bh[i]); 10079cb8cedSChan Jeong } 10179cb8cedSChan Jeong 10279cb8cedSChan Jeong res = lzo1x_decompress_safe(stream->input, (size_t)length, 10379cb8cedSChan Jeong stream->output, &out_len); 10479cb8cedSChan Jeong if (res != LZO_E_OK) 10579cb8cedSChan Jeong goto failed; 10679cb8cedSChan Jeong 10779cb8cedSChan Jeong res = bytes = (int)out_len; 10879cb8cedSChan Jeong for (i = 0, buff = stream->output; bytes && i < pages; i++) { 10979cb8cedSChan Jeong avail = min_t(int, bytes, PAGE_CACHE_SIZE); 11079cb8cedSChan Jeong memcpy(buffer[i], buff, avail); 11179cb8cedSChan Jeong buff += avail; 11279cb8cedSChan Jeong bytes -= avail; 11379cb8cedSChan Jeong } 11479cb8cedSChan Jeong 11579cb8cedSChan Jeong mutex_unlock(&msblk->read_data_mutex); 11679cb8cedSChan Jeong return res; 11779cb8cedSChan Jeong 11879cb8cedSChan Jeong block_release: 11979cb8cedSChan Jeong for (; i < b; i++) 12079cb8cedSChan Jeong put_bh(bh[i]); 12179cb8cedSChan Jeong 12279cb8cedSChan Jeong failed: 12379cb8cedSChan Jeong mutex_unlock(&msblk->read_data_mutex); 12479cb8cedSChan Jeong 12579cb8cedSChan Jeong ERROR("lzo decompression failed, data probably corrupt\n"); 12679cb8cedSChan Jeong return -EIO; 12779cb8cedSChan Jeong } 12879cb8cedSChan Jeong 12979cb8cedSChan Jeong const struct squashfs_decompressor squashfs_lzo_comp_ops = { 13079cb8cedSChan Jeong .init = lzo_init, 13179cb8cedSChan Jeong .free = lzo_free, 13279cb8cedSChan Jeong .decompress = lzo_uncompress, 13379cb8cedSChan Jeong .id = LZO_COMPRESSION, 13479cb8cedSChan Jeong .name = "lzo", 13579cb8cedSChan Jeong .supported = 1 13679cb8cedSChan Jeong }; 137