blob: df4fa3c7ddd0d31710e51e980b9a8582510a711f [file] [log] [blame]
Phillip Lougher9c06a462014-11-27 06:49:14 +00001/*
2 * Copyright (c) 2013, 2014
3 * Phillip Lougher <phillip@squashfs.org.uk>
4 *
5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
7 */
8
9#include <linux/buffer_head.h>
10#include <linux/mutex.h>
11#include <linux/slab.h>
12#include <linux/vmalloc.h>
13#include <linux/lz4.h>
14
15#include "squashfs_fs.h"
16#include "squashfs_fs_sb.h"
17#include "squashfs.h"
18#include "decompressor.h"
19#include "page_actor.h"
20
21#define LZ4_LEGACY 1
22
23struct lz4_comp_opts {
24 __le32 version;
25 __le32 flags;
26};
27
28struct squashfs_lz4 {
29 void *input;
30 void *output;
31};
32
33
34static void *lz4_comp_opts(struct squashfs_sb_info *msblk,
35 void *buff, int len)
36{
37 struct lz4_comp_opts *comp_opts = buff;
38
39 /* LZ4 compressed filesystems always have compression options */
40 if (comp_opts == NULL || len < sizeof(*comp_opts))
41 return ERR_PTR(-EIO);
42
43 if (le32_to_cpu(comp_opts->version) != LZ4_LEGACY) {
44 /* LZ4 format currently used by the kernel is the 'legacy'
45 * format */
46 ERROR("Unknown LZ4 version\n");
47 return ERR_PTR(-EINVAL);
48 }
49
50 return NULL;
51}
52
53
54static void *lz4_init(struct squashfs_sb_info *msblk, void *buff)
55{
56 int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
57 struct squashfs_lz4 *stream;
58
59 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
60 if (stream == NULL)
61 goto failed;
62 stream->input = vmalloc(block_size);
63 if (stream->input == NULL)
64 goto failed2;
65 stream->output = vmalloc(block_size);
66 if (stream->output == NULL)
67 goto failed3;
68
69 return stream;
70
71failed3:
72 vfree(stream->input);
73failed2:
74 kfree(stream);
75failed:
76 ERROR("Failed to initialise LZ4 decompressor\n");
77 return ERR_PTR(-ENOMEM);
78}
79
80
81static void lz4_free(void *strm)
82{
83 struct squashfs_lz4 *stream = strm;
84
85 if (stream) {
86 vfree(stream->input);
87 vfree(stream->output);
88 }
89 kfree(stream);
90}
91
92
93static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,
94 struct buffer_head **bh, int b, int offset, int length,
95 struct squashfs_page_actor *output)
96{
Adrien Schildknecht38840af2016-09-28 13:59:18 -070097 int res;
Phillip Lougher9c06a462014-11-27 06:49:14 +000098 size_t dest_len = output->length;
Adrien Schildknecht38840af2016-09-28 13:59:18 -070099 struct squashfs_lz4 *stream = strm;
Phillip Lougher9c06a462014-11-27 06:49:14 +0000100
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700101 squashfs_bh_to_buf(bh, b, stream->input, offset, length,
102 msblk->devblksize);
Phillip Lougher9c06a462014-11-27 06:49:14 +0000103 res = lz4_decompress_unknownoutputsize(stream->input, length,
104 stream->output, &dest_len);
105 if (res)
106 return -EIO;
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700107 squashfs_buf_to_actor(stream->output, output, dest_len);
Phillip Lougher9c06a462014-11-27 06:49:14 +0000108
109 return dest_len;
110}
111
112const struct squashfs_decompressor squashfs_lz4_comp_ops = {
113 .init = lz4_init,
114 .comp_opts = lz4_comp_opts,
115 .free = lz4_free,
116 .decompress = lz4_uncompress,
117 .id = LZ4_COMPRESSION,
118 .name = "lz4",
119 .supported = 1
120};