Commit | Line | Data |
---|---|---|
68252eb5 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
79cb8ced CJ |
2 | /* |
3 | * Squashfs - a compressed read only filesystem for Linux | |
4 | * | |
5 | * Copyright (c) 2010 LG Electronics | |
6 | * Chan Jeong <chan.jeong@lge.com> | |
7 | * | |
79cb8ced CJ |
8 | * lzo_wrapper.c |
9 | */ | |
10 | ||
11 | #include <linux/mutex.h> | |
93e72b3c | 12 | #include <linux/bio.h> |
79cb8ced CJ |
13 | #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" | |
79cb8ced CJ |
19 | #include "squashfs.h" |
20 | #include "decompressor.h" | |
846b730e | 21 | #include "page_actor.h" |
79cb8ced CJ |
22 | |
23 | struct squashfs_lzo { | |
24 | void *input; | |
25 | void *output; | |
26 | }; | |
27 | ||
9508c6b9 | 28 | static void *lzo_init(struct squashfs_sb_info *msblk, void *buff) |
79cb8ced | 29 | { |
f3065f60 PL |
30 | int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE); |
31 | ||
79cb8ced CJ |
32 | struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
33 | if (stream == NULL) | |
34 | goto failed; | |
f3065f60 | 35 | stream->input = vmalloc(block_size); |
79cb8ced CJ |
36 | if (stream->input == NULL) |
37 | goto failed; | |
f3065f60 | 38 | stream->output = vmalloc(block_size); |
79cb8ced CJ |
39 | if (stream->output == NULL) |
40 | goto failed2; | |
41 | ||
42 | return stream; | |
43 | ||
44 | failed2: | |
45 | vfree(stream->input); | |
46 | failed: | |
47 | ERROR("Failed to allocate lzo workspace\n"); | |
48 | kfree(stream); | |
b7fc0ff0 | 49 | return ERR_PTR(-ENOMEM); |
79cb8ced CJ |
50 | } |
51 | ||
52 | ||
53 | static 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 | ||
9508c6b9 | 65 | static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm, |
93e72b3c | 66 | struct bio *bio, int offset, int length, |
846b730e | 67 | struct squashfs_page_actor *output) |
79cb8ced | 68 | { |
93e72b3c PL |
69 | struct bvec_iter_all iter_all = {}; |
70 | struct bio_vec *bvec = bvec_init_iter_all(&iter_all); | |
9508c6b9 | 71 | struct squashfs_lzo *stream = strm; |
846b730e | 72 | void *buff = stream->input, *data; |
93e72b3c | 73 | int bytes = length, res; |
846b730e | 74 | size_t out_len = output->length; |
79cb8ced | 75 | |
93e72b3c PL |
76 | while (bio_next_segment(bio, &iter_all)) { |
77 | int avail = min(bytes, ((int)bvec->bv_len) - offset); | |
78 | ||
fbc27241 | 79 | data = bvec_virt(bvec); |
93e72b3c | 80 | memcpy(buff, data + offset, avail); |
79cb8ced CJ |
81 | buff += avail; |
82 | bytes -= avail; | |
83 | offset = 0; | |
79cb8ced CJ |
84 | } |
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; | |
846b730e PL |
92 | data = squashfs_first_page(output); |
93 | buff = stream->output; | |
94 | while (data) { | |
09cbfeaf | 95 | if (bytes <= PAGE_SIZE) { |
f268eedd PL |
96 | if (!IS_ERR(data)) |
97 | memcpy(data, buff, bytes); | |
846b730e PL |
98 | break; |
99 | } else { | |
f268eedd PL |
100 | if (!IS_ERR(data)) |
101 | memcpy(data, buff, PAGE_SIZE); | |
09cbfeaf KS |
102 | buff += PAGE_SIZE; |
103 | bytes -= PAGE_SIZE; | |
846b730e PL |
104 | data = squashfs_next_page(output); |
105 | } | |
79cb8ced | 106 | } |
846b730e | 107 | squashfs_finish_page(output); |
79cb8ced | 108 | |
79cb8ced CJ |
109 | return res; |
110 | ||
79cb8ced | 111 | failed: |
79cb8ced CJ |
112 | return -EIO; |
113 | } | |
114 | ||
115 | const struct squashfs_decompressor squashfs_lzo_comp_ops = { | |
116 | .init = lzo_init, | |
117 | .free = lzo_free, | |
118 | .decompress = lzo_uncompress, | |
119 | .id = LZO_COMPRESSION, | |
120 | .name = "lzo", | |
f268eedd | 121 | .alloc_buffer = 0, |
79cb8ced CJ |
122 | .supported = 1 |
123 | }; |