blob: cb510a631968364b247674ed62746363ebcf9d28 [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Chan Jeong79cb8ce2010-08-05 02:29:59 +01002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2010 LG Electronics
6 * Chan Jeong <chan.jeong@lge.com>
7 *
Chan Jeong79cb8ce2010-08-05 02:29:59 +01008 * lzo_wrapper.c
9 */
10
11#include <linux/mutex.h>
Philippe Liard93e72b32020-06-01 21:45:23 -070012#include <linux/bio.h>
Chan Jeong79cb8ce2010-08-05 02:29:59 +010013#include <linux/slab.h>
14#include <linux/vmalloc.h>
15#include <linux/lzo.h>
16
17#include "squashfs_fs.h"
18#include "squashfs_fs_sb.h"
Chan Jeong79cb8ce2010-08-05 02:29:59 +010019#include "squashfs.h"
20#include "decompressor.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000021#include "page_actor.h"
Chan Jeong79cb8ce2010-08-05 02:29:59 +010022
23struct squashfs_lzo {
24 void *input;
25 void *output;
26};
27
Phillip Lougher9508c6b2013-11-13 02:56:26 +000028static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
Chan Jeong79cb8ce2010-08-05 02:29:59 +010029{
Phillip Lougherf3065f62010-08-05 04:51:50 +010030 int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
31
Chan Jeong79cb8ce2010-08-05 02:29:59 +010032 struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
33 if (stream == NULL)
34 goto failed;
Phillip Lougherf3065f62010-08-05 04:51:50 +010035 stream->input = vmalloc(block_size);
Chan Jeong79cb8ce2010-08-05 02:29:59 +010036 if (stream->input == NULL)
37 goto failed;
Phillip Lougherf3065f62010-08-05 04:51:50 +010038 stream->output = vmalloc(block_size);
Chan Jeong79cb8ce2010-08-05 02:29:59 +010039 if (stream->output == NULL)
40 goto failed2;
41
42 return stream;
43
44failed2:
45 vfree(stream->input);
46failed:
47 ERROR("Failed to allocate lzo workspace\n");
48 kfree(stream);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000049 return ERR_PTR(-ENOMEM);
Chan Jeong79cb8ce2010-08-05 02:29:59 +010050}
51
52
53static void lzo_free(void *strm)
54{
55 struct squashfs_lzo *stream = strm;
56
57 if (stream) {
58 vfree(stream->input);
59 vfree(stream->output);
60 }
61 kfree(stream);
62}
63
64
Phillip Lougher9508c6b2013-11-13 02:56:26 +000065static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
Philippe Liard93e72b32020-06-01 21:45:23 -070066 struct bio *bio, int offset, int length,
Phillip Lougher846b7302013-11-18 02:59:12 +000067 struct squashfs_page_actor *output)
Chan Jeong79cb8ce2010-08-05 02:29:59 +010068{
Philippe Liard93e72b32020-06-01 21:45:23 -070069 struct bvec_iter_all iter_all = {};
70 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
Phillip Lougher9508c6b2013-11-13 02:56:26 +000071 struct squashfs_lzo *stream = strm;
Phillip Lougher846b7302013-11-18 02:59:12 +000072 void *buff = stream->input, *data;
Philippe Liard93e72b32020-06-01 21:45:23 -070073 int bytes = length, res;
Phillip Lougher846b7302013-11-18 02:59:12 +000074 size_t out_len = output->length;
Chan Jeong79cb8ce2010-08-05 02:29:59 +010075
Philippe Liard93e72b32020-06-01 21:45:23 -070076 while (bio_next_segment(bio, &iter_all)) {
77 int avail = min(bytes, ((int)bvec->bv_len) - offset);
78
Christoph Hellwigfbc272412021-08-04 11:56:25 +020079 data = bvec_virt(bvec);
Philippe Liard93e72b32020-06-01 21:45:23 -070080 memcpy(buff, data + offset, avail);
Chan Jeong79cb8ce2010-08-05 02:29:59 +010081 buff += avail;
82 bytes -= avail;
83 offset = 0;
Chan Jeong79cb8ce2010-08-05 02:29:59 +010084 }
85
86 res = lzo1x_decompress_safe(stream->input, (size_t)length,
87 stream->output, &out_len);
88 if (res != LZO_E_OK)
89 goto failed;
90
91 res = bytes = (int)out_len;
Phillip Lougher846b7302013-11-18 02:59:12 +000092 data = squashfs_first_page(output);
93 buff = stream->output;
94 while (data) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030095 if (bytes <= PAGE_SIZE) {
Phillip Lougher846b7302013-11-18 02:59:12 +000096 memcpy(data, buff, bytes);
97 break;
98 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 memcpy(data, buff, PAGE_SIZE);
100 buff += PAGE_SIZE;
101 bytes -= PAGE_SIZE;
Phillip Lougher846b7302013-11-18 02:59:12 +0000102 data = squashfs_next_page(output);
103 }
Chan Jeong79cb8ce2010-08-05 02:29:59 +0100104 }
Phillip Lougher846b7302013-11-18 02:59:12 +0000105 squashfs_finish_page(output);
Chan Jeong79cb8ce2010-08-05 02:29:59 +0100106
Chan Jeong79cb8ce2010-08-05 02:29:59 +0100107 return res;
108
Chan Jeong79cb8ce2010-08-05 02:29:59 +0100109failed:
Chan Jeong79cb8ce2010-08-05 02:29:59 +0100110 return -EIO;
111}
112
113const struct squashfs_decompressor squashfs_lzo_comp_ops = {
114 .init = lzo_init,
115 .free = lzo_free,
116 .decompress = lzo_uncompress,
117 .id = LZO_COMPRESSION,
118 .name = "lzo",
119 .supported = 1
120};