1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2010 LG Electronics
6 * Chan Jeong <chan.jeong@lge.com>
11 #include <linux/mutex.h>
12 #include <linux/bio.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/lzo.h>
17 #include "squashfs_fs.h"
18 #include "squashfs_fs_sb.h"
20 #include "decompressor.h"
21 #include "page_actor.h"
28 static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
30 int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
32 struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
35 stream->input = vmalloc(block_size);
36 if (stream->input == NULL)
38 stream->output = vmalloc(block_size);
39 if (stream->output == NULL)
47 ERROR("Failed to allocate lzo workspace\n");
49 return ERR_PTR(-ENOMEM);
53 static void lzo_free(void *strm)
55 struct squashfs_lzo *stream = strm;
59 vfree(stream->output);
65 static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
66 struct bio *bio, int offset, int length,
67 struct squashfs_page_actor *output)
69 struct bvec_iter_all iter_all = {};
70 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
71 struct squashfs_lzo *stream = strm;
72 void *buff = stream->input, *data;
73 int bytes = length, res;
74 size_t out_len = output->length;
76 while (bio_next_segment(bio, &iter_all)) {
77 int avail = min(bytes, ((int)bvec->bv_len) - offset);
79 data = bvec_virt(bvec);
80 memcpy(buff, data + offset, avail);
86 res = lzo1x_decompress_safe(stream->input, (size_t)length,
87 stream->output, &out_len);
91 res = bytes = (int)out_len;
92 data = squashfs_first_page(output);
93 buff = stream->output;
95 if (bytes <= PAGE_SIZE) {
97 memcpy(data, buff, bytes);
101 memcpy(data, buff, PAGE_SIZE);
104 data = squashfs_next_page(output);
107 squashfs_finish_page(output);
115 const struct squashfs_decompressor squashfs_lzo_comp_ops = {
118 .decompress = lzo_uncompress,
119 .id = LZO_COMPRESSION,