xref: /openbmc/linux/fs/squashfs/lzo_wrapper.c (revision 846b730e)
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.h"
3379cb8cedSChan Jeong #include "decompressor.h"
34846b730eSPhillip Lougher #include "page_actor.h"
3579cb8cedSChan Jeong 
3679cb8cedSChan Jeong struct squashfs_lzo {
3779cb8cedSChan Jeong 	void	*input;
3879cb8cedSChan Jeong 	void	*output;
3979cb8cedSChan Jeong };
4079cb8cedSChan Jeong 
419508c6b9SPhillip Lougher static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
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);
62b7fc0ff0SPhillip Lougher 	return ERR_PTR(-ENOMEM);
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 
789508c6b9SPhillip Lougher static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
79846b730eSPhillip Lougher 	struct buffer_head **bh, int b, int offset, int length,
80846b730eSPhillip Lougher 	struct squashfs_page_actor *output)
8179cb8cedSChan Jeong {
829508c6b9SPhillip Lougher 	struct squashfs_lzo *stream = strm;
83846b730eSPhillip Lougher 	void *buff = stream->input, *data;
8479cb8cedSChan Jeong 	int avail, i, bytes = length, res;
85846b730eSPhillip Lougher 	size_t out_len = output->length;
8679cb8cedSChan Jeong 
8779cb8cedSChan Jeong 	for (i = 0; i < b; i++) {
8879cb8cedSChan Jeong 		avail = min(bytes, msblk->devblksize - offset);
8979cb8cedSChan Jeong 		memcpy(buff, bh[i]->b_data + offset, avail);
9079cb8cedSChan Jeong 		buff += avail;
9179cb8cedSChan Jeong 		bytes -= avail;
9279cb8cedSChan Jeong 		offset = 0;
9379cb8cedSChan Jeong 		put_bh(bh[i]);
9479cb8cedSChan Jeong 	}
9579cb8cedSChan Jeong 
9679cb8cedSChan Jeong 	res = lzo1x_decompress_safe(stream->input, (size_t)length,
9779cb8cedSChan Jeong 					stream->output, &out_len);
9879cb8cedSChan Jeong 	if (res != LZO_E_OK)
9979cb8cedSChan Jeong 		goto failed;
10079cb8cedSChan Jeong 
10179cb8cedSChan Jeong 	res = bytes = (int)out_len;
102846b730eSPhillip Lougher 	data = squashfs_first_page(output);
103846b730eSPhillip Lougher 	buff = stream->output;
104846b730eSPhillip Lougher 	while (data) {
105846b730eSPhillip Lougher 		if (bytes <= PAGE_CACHE_SIZE) {
106846b730eSPhillip Lougher 			memcpy(data, buff, bytes);
107846b730eSPhillip Lougher 			break;
108846b730eSPhillip Lougher 		} else {
109846b730eSPhillip Lougher 			memcpy(data, buff, PAGE_CACHE_SIZE);
110846b730eSPhillip Lougher 			buff += PAGE_CACHE_SIZE;
111846b730eSPhillip Lougher 			bytes -= PAGE_CACHE_SIZE;
112846b730eSPhillip Lougher 			data = squashfs_next_page(output);
11379cb8cedSChan Jeong 		}
114846b730eSPhillip Lougher 	}
115846b730eSPhillip Lougher 	squashfs_finish_page(output);
11679cb8cedSChan Jeong 
11779cb8cedSChan Jeong 	return res;
11879cb8cedSChan Jeong 
11979cb8cedSChan Jeong failed:
12079cb8cedSChan Jeong 	return -EIO;
12179cb8cedSChan Jeong }
12279cb8cedSChan Jeong 
12379cb8cedSChan Jeong const struct squashfs_decompressor squashfs_lzo_comp_ops = {
12479cb8cedSChan Jeong 	.init = lzo_init,
12579cb8cedSChan Jeong 	.free = lzo_free,
12679cb8cedSChan Jeong 	.decompress = lzo_uncompress,
12779cb8cedSChan Jeong 	.id = LZO_COMPRESSION,
12879cb8cedSChan Jeong 	.name = "lzo",
12979cb8cedSChan Jeong 	.supported = 1
13079cb8cedSChan Jeong };
131