xref: /openbmc/linux/fs/squashfs/lzo_wrapper.c (revision 93e72b3c)
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>
1293e72b3cSPhilippe Liard #include <linux/bio.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,
6693e72b3cSPhilippe Liard 	struct bio *bio, int offset, int length,
67846b730eSPhillip Lougher 	struct squashfs_page_actor *output)
6879cb8cedSChan Jeong {
6993e72b3cSPhilippe Liard 	struct bvec_iter_all iter_all = {};
7093e72b3cSPhilippe Liard 	struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
719508c6b9SPhillip Lougher 	struct squashfs_lzo *stream = strm;
72846b730eSPhillip Lougher 	void *buff = stream->input, *data;
7393e72b3cSPhilippe Liard 	int bytes = length, res;
74846b730eSPhillip Lougher 	size_t out_len = output->length;
7579cb8cedSChan Jeong 
7693e72b3cSPhilippe Liard 	while (bio_next_segment(bio, &iter_all)) {
7793e72b3cSPhilippe Liard 		int avail = min(bytes, ((int)bvec->bv_len) - offset);
7893e72b3cSPhilippe Liard 
7993e72b3cSPhilippe Liard 		data = page_address(bvec->bv_page) + bvec->bv_offset;
8093e72b3cSPhilippe Liard 		memcpy(buff, data + offset, avail);
8179cb8cedSChan Jeong 		buff += avail;
8279cb8cedSChan Jeong 		bytes -= avail;
8379cb8cedSChan Jeong 		offset = 0;
8479cb8cedSChan Jeong 	}
8579cb8cedSChan Jeong 
8679cb8cedSChan Jeong 	res = lzo1x_decompress_safe(stream->input, (size_t)length,
8779cb8cedSChan Jeong 					stream->output, &out_len);
8879cb8cedSChan Jeong 	if (res != LZO_E_OK)
8979cb8cedSChan Jeong 		goto failed;
9079cb8cedSChan Jeong 
9179cb8cedSChan Jeong 	res = bytes = (int)out_len;
92846b730eSPhillip Lougher 	data = squashfs_first_page(output);
93846b730eSPhillip Lougher 	buff = stream->output;
94846b730eSPhillip Lougher 	while (data) {
9509cbfeafSKirill A. Shutemov 		if (bytes <= PAGE_SIZE) {
96846b730eSPhillip Lougher 			memcpy(data, buff, bytes);
97846b730eSPhillip Lougher 			break;
98846b730eSPhillip Lougher 		} else {
9909cbfeafSKirill A. Shutemov 			memcpy(data, buff, PAGE_SIZE);
10009cbfeafSKirill A. Shutemov 			buff += PAGE_SIZE;
10109cbfeafSKirill A. Shutemov 			bytes -= PAGE_SIZE;
102846b730eSPhillip Lougher 			data = squashfs_next_page(output);
10379cb8cedSChan Jeong 		}
104846b730eSPhillip Lougher 	}
105846b730eSPhillip Lougher 	squashfs_finish_page(output);
10679cb8cedSChan Jeong 
10779cb8cedSChan Jeong 	return res;
10879cb8cedSChan Jeong 
10979cb8cedSChan Jeong failed:
11079cb8cedSChan Jeong 	return -EIO;
11179cb8cedSChan Jeong }
11279cb8cedSChan Jeong 
11379cb8cedSChan Jeong const struct squashfs_decompressor squashfs_lzo_comp_ops = {
11479cb8cedSChan Jeong 	.init = lzo_init,
11579cb8cedSChan Jeong 	.free = lzo_free,
11679cb8cedSChan Jeong 	.decompress = lzo_uncompress,
11779cb8cedSChan Jeong 	.id = LZO_COMPRESSION,
11879cb8cedSChan Jeong 	.name = "lzo",
11979cb8cedSChan Jeong 	.supported = 1
12079cb8cedSChan Jeong };
121