xref: /openbmc/linux/fs/squashfs/lzo_wrapper.c (revision 79cb8ced)
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 {
4379cb8cedSChan Jeong 	struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
4479cb8cedSChan Jeong 	if (stream == NULL)
4579cb8cedSChan Jeong 		goto failed;
4679cb8cedSChan Jeong 	stream->input = vmalloc(msblk->block_size);
4779cb8cedSChan Jeong 	if (stream->input == NULL)
4879cb8cedSChan Jeong 		goto failed;
4979cb8cedSChan Jeong 	stream->output = vmalloc(msblk->block_size);
5079cb8cedSChan Jeong 	if (stream->output == NULL)
5179cb8cedSChan Jeong 		goto failed2;
5279cb8cedSChan Jeong 
5379cb8cedSChan Jeong 	return stream;
5479cb8cedSChan Jeong 
5579cb8cedSChan Jeong failed2:
5679cb8cedSChan Jeong 	vfree(stream->input);
5779cb8cedSChan Jeong failed:
5879cb8cedSChan Jeong 	ERROR("Failed to allocate lzo workspace\n");
5979cb8cedSChan Jeong 	kfree(stream);
6079cb8cedSChan Jeong 	return NULL;
6179cb8cedSChan Jeong }
6279cb8cedSChan Jeong 
6379cb8cedSChan Jeong 
6479cb8cedSChan Jeong static void lzo_free(void *strm)
6579cb8cedSChan Jeong {
6679cb8cedSChan Jeong 	struct squashfs_lzo *stream = strm;
6779cb8cedSChan Jeong 
6879cb8cedSChan Jeong 	if (stream) {
6979cb8cedSChan Jeong 		vfree(stream->input);
7079cb8cedSChan Jeong 		vfree(stream->output);
7179cb8cedSChan Jeong 	}
7279cb8cedSChan Jeong 	kfree(stream);
7379cb8cedSChan Jeong }
7479cb8cedSChan Jeong 
7579cb8cedSChan Jeong 
7679cb8cedSChan Jeong static int lzo_uncompress(struct squashfs_sb_info *msblk, void **buffer,
7779cb8cedSChan Jeong 	struct buffer_head **bh, int b, int offset, int length, int srclength,
7879cb8cedSChan Jeong 	int pages)
7979cb8cedSChan Jeong {
8079cb8cedSChan Jeong 	struct squashfs_lzo *stream = msblk->stream;
8179cb8cedSChan Jeong 	void *buff = stream->input;
8279cb8cedSChan Jeong 	int avail, i, bytes = length, res;
8379cb8cedSChan Jeong 	size_t out_len = msblk->block_size;
8479cb8cedSChan Jeong 
8579cb8cedSChan Jeong 	mutex_lock(&msblk->read_data_mutex);
8679cb8cedSChan Jeong 
8779cb8cedSChan Jeong 	for (i = 0; i < b; i++) {
8879cb8cedSChan Jeong 		wait_on_buffer(bh[i]);
8979cb8cedSChan Jeong 		if (!buffer_uptodate(bh[i]))
9079cb8cedSChan Jeong 			goto block_release;
9179cb8cedSChan Jeong 
9279cb8cedSChan Jeong 		avail = min(bytes, msblk->devblksize - offset);
9379cb8cedSChan Jeong 		memcpy(buff, bh[i]->b_data + offset, avail);
9479cb8cedSChan Jeong 		buff += avail;
9579cb8cedSChan Jeong 		bytes -= avail;
9679cb8cedSChan Jeong 		offset = 0;
9779cb8cedSChan Jeong 		put_bh(bh[i]);
9879cb8cedSChan Jeong 	}
9979cb8cedSChan Jeong 
10079cb8cedSChan Jeong 	res = lzo1x_decompress_safe(stream->input, (size_t)length,
10179cb8cedSChan Jeong 					stream->output, &out_len);
10279cb8cedSChan Jeong 	if (res != LZO_E_OK)
10379cb8cedSChan Jeong 		goto failed;
10479cb8cedSChan Jeong 
10579cb8cedSChan Jeong 	res = bytes = (int)out_len;
10679cb8cedSChan Jeong 	for (i = 0, buff = stream->output; bytes && i < pages; i++) {
10779cb8cedSChan Jeong 		avail = min_t(int, bytes, PAGE_CACHE_SIZE);
10879cb8cedSChan Jeong 		memcpy(buffer[i], buff, avail);
10979cb8cedSChan Jeong 		buff += avail;
11079cb8cedSChan Jeong 		bytes -= avail;
11179cb8cedSChan Jeong 	}
11279cb8cedSChan Jeong 
11379cb8cedSChan Jeong 	mutex_unlock(&msblk->read_data_mutex);
11479cb8cedSChan Jeong 	return res;
11579cb8cedSChan Jeong 
11679cb8cedSChan Jeong block_release:
11779cb8cedSChan Jeong 	for (; i < b; i++)
11879cb8cedSChan Jeong 		put_bh(bh[i]);
11979cb8cedSChan Jeong 
12079cb8cedSChan Jeong failed:
12179cb8cedSChan Jeong 	mutex_unlock(&msblk->read_data_mutex);
12279cb8cedSChan Jeong 
12379cb8cedSChan Jeong 	ERROR("lzo decompression failed, data probably corrupt\n");
12479cb8cedSChan Jeong 	return -EIO;
12579cb8cedSChan Jeong }
12679cb8cedSChan Jeong 
12779cb8cedSChan Jeong const struct squashfs_decompressor squashfs_lzo_comp_ops = {
12879cb8cedSChan Jeong 	.init = lzo_init,
12979cb8cedSChan Jeong 	.free = lzo_free,
13079cb8cedSChan Jeong 	.decompress = lzo_uncompress,
13179cb8cedSChan Jeong 	.id = LZO_COMPRESSION,
13279cb8cedSChan Jeong 	.name = "lzo",
13379cb8cedSChan Jeong 	.supported = 1
13479cb8cedSChan Jeong };
135