xref: /openbmc/linux/fs/squashfs/lzo_wrapper.c (revision 68252eb5)
168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
279cb8cedSChan Jeong /*
379cb8cedSChan Jeong  * Squashfs - a compressed read only filesystem for Linux
479cb8cedSChan Jeong  *
579cb8cedSChan Jeong  * Copyright (c) 2010 LG Electronics
679cb8cedSChan Jeong  * Chan Jeong <chan.jeong@lge.com>
779cb8cedSChan Jeong  *
879cb8cedSChan Jeong  * lzo_wrapper.c
979cb8cedSChan Jeong  */
1079cb8cedSChan Jeong 
1179cb8cedSChan Jeong #include <linux/mutex.h>
1279cb8cedSChan Jeong #include <linux/buffer_head.h>
1379cb8cedSChan Jeong #include <linux/slab.h>
1479cb8cedSChan Jeong #include <linux/vmalloc.h>
1579cb8cedSChan Jeong #include <linux/lzo.h>
1679cb8cedSChan Jeong 
1779cb8cedSChan Jeong #include "squashfs_fs.h"
1879cb8cedSChan Jeong #include "squashfs_fs_sb.h"
1979cb8cedSChan Jeong #include "squashfs.h"
2079cb8cedSChan Jeong #include "decompressor.h"
21846b730eSPhillip Lougher #include "page_actor.h"
2279cb8cedSChan Jeong 
2379cb8cedSChan Jeong struct squashfs_lzo {
2479cb8cedSChan Jeong 	void	*input;
2579cb8cedSChan Jeong 	void	*output;
2679cb8cedSChan Jeong };
2779cb8cedSChan Jeong 
289508c6b9SPhillip Lougher static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
2979cb8cedSChan Jeong {
30f3065f60SPhillip Lougher 	int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
31f3065f60SPhillip Lougher 
3279cb8cedSChan Jeong 	struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
3379cb8cedSChan Jeong 	if (stream == NULL)
3479cb8cedSChan Jeong 		goto failed;
35f3065f60SPhillip Lougher 	stream->input = vmalloc(block_size);
3679cb8cedSChan Jeong 	if (stream->input == NULL)
3779cb8cedSChan Jeong 		goto failed;
38f3065f60SPhillip Lougher 	stream->output = vmalloc(block_size);
3979cb8cedSChan Jeong 	if (stream->output == NULL)
4079cb8cedSChan Jeong 		goto failed2;
4179cb8cedSChan Jeong 
4279cb8cedSChan Jeong 	return stream;
4379cb8cedSChan Jeong 
4479cb8cedSChan Jeong failed2:
4579cb8cedSChan Jeong 	vfree(stream->input);
4679cb8cedSChan Jeong failed:
4779cb8cedSChan Jeong 	ERROR("Failed to allocate lzo workspace\n");
4879cb8cedSChan Jeong 	kfree(stream);
49b7fc0ff0SPhillip Lougher 	return ERR_PTR(-ENOMEM);
5079cb8cedSChan Jeong }
5179cb8cedSChan Jeong 
5279cb8cedSChan Jeong 
5379cb8cedSChan Jeong static void lzo_free(void *strm)
5479cb8cedSChan Jeong {
5579cb8cedSChan Jeong 	struct squashfs_lzo *stream = strm;
5679cb8cedSChan Jeong 
5779cb8cedSChan Jeong 	if (stream) {
5879cb8cedSChan Jeong 		vfree(stream->input);
5979cb8cedSChan Jeong 		vfree(stream->output);
6079cb8cedSChan Jeong 	}
6179cb8cedSChan Jeong 	kfree(stream);
6279cb8cedSChan Jeong }
6379cb8cedSChan Jeong 
6479cb8cedSChan Jeong 
659508c6b9SPhillip Lougher static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
66846b730eSPhillip Lougher 	struct buffer_head **bh, int b, int offset, int length,
67846b730eSPhillip Lougher 	struct squashfs_page_actor *output)
6879cb8cedSChan Jeong {
699508c6b9SPhillip Lougher 	struct squashfs_lzo *stream = strm;
70846b730eSPhillip Lougher 	void *buff = stream->input, *data;
7179cb8cedSChan Jeong 	int avail, i, bytes = length, res;
72846b730eSPhillip Lougher 	size_t out_len = output->length;
7379cb8cedSChan Jeong 
7479cb8cedSChan Jeong 	for (i = 0; i < b; i++) {
7579cb8cedSChan Jeong 		avail = min(bytes, msblk->devblksize - offset);
7679cb8cedSChan Jeong 		memcpy(buff, bh[i]->b_data + offset, avail);
7779cb8cedSChan Jeong 		buff += avail;
7879cb8cedSChan Jeong 		bytes -= avail;
7979cb8cedSChan Jeong 		offset = 0;
8079cb8cedSChan Jeong 		put_bh(bh[i]);
8179cb8cedSChan Jeong 	}
8279cb8cedSChan Jeong 
8379cb8cedSChan Jeong 	res = lzo1x_decompress_safe(stream->input, (size_t)length,
8479cb8cedSChan Jeong 					stream->output, &out_len);
8579cb8cedSChan Jeong 	if (res != LZO_E_OK)
8679cb8cedSChan Jeong 		goto failed;
8779cb8cedSChan Jeong 
8879cb8cedSChan Jeong 	res = bytes = (int)out_len;
89846b730eSPhillip Lougher 	data = squashfs_first_page(output);
90846b730eSPhillip Lougher 	buff = stream->output;
91846b730eSPhillip Lougher 	while (data) {
9209cbfeafSKirill A. Shutemov 		if (bytes <= PAGE_SIZE) {
93846b730eSPhillip Lougher 			memcpy(data, buff, bytes);
94846b730eSPhillip Lougher 			break;
95846b730eSPhillip Lougher 		} else {
9609cbfeafSKirill A. Shutemov 			memcpy(data, buff, PAGE_SIZE);
9709cbfeafSKirill A. Shutemov 			buff += PAGE_SIZE;
9809cbfeafSKirill A. Shutemov 			bytes -= PAGE_SIZE;
99846b730eSPhillip Lougher 			data = squashfs_next_page(output);
10079cb8cedSChan Jeong 		}
101846b730eSPhillip Lougher 	}
102846b730eSPhillip Lougher 	squashfs_finish_page(output);
10379cb8cedSChan Jeong 
10479cb8cedSChan Jeong 	return res;
10579cb8cedSChan Jeong 
10679cb8cedSChan Jeong failed:
10779cb8cedSChan Jeong 	return -EIO;
10879cb8cedSChan Jeong }
10979cb8cedSChan Jeong 
11079cb8cedSChan Jeong const struct squashfs_decompressor squashfs_lzo_comp_ops = {
11179cb8cedSChan Jeong 	.init = lzo_init,
11279cb8cedSChan Jeong 	.free = lzo_free,
11379cb8cedSChan Jeong 	.decompress = lzo_uncompress,
11479cb8cedSChan Jeong 	.id = LZO_COMPRESSION,
11579cb8cedSChan Jeong 	.name = "lzo",
11679cb8cedSChan Jeong 	.supported = 1
11779cb8cedSChan Jeong };
118