blob: 68f6d09bb3a2bc0388b3c34bd303107c5d0968ee [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Lougher81bb8de2010-12-09 02:02:29 +00002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher81bb8de2010-12-09 02:02:29 +00007 *
Phillip Lougher81bb8de2010-12-09 02:02:29 +00008 * xz_wrapper.c
9 */
10
11
12#include <linux/mutex.h>
Philippe Liard93e72b32020-06-01 21:45:23 -070013#include <linux/bio.h>
Phillip Lougher81bb8de2010-12-09 02:02:29 +000014#include <linux/slab.h>
15#include <linux/xz.h>
Phillip Lougherff750312011-02-28 15:31:46 +000016#include <linux/bitops.h>
Phillip Lougher81bb8de2010-12-09 02:02:29 +000017
18#include "squashfs_fs.h"
19#include "squashfs_fs_sb.h"
Phillip Lougher81bb8de2010-12-09 02:02:29 +000020#include "squashfs.h"
21#include "decompressor.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000022#include "page_actor.h"
Phillip Lougher81bb8de2010-12-09 02:02:29 +000023
24struct squashfs_xz {
25 struct xz_dec *state;
26 struct xz_buf buf;
27};
28
Phillip Lougher9508c6b2013-11-13 02:56:26 +000029struct disk_comp_opts {
Phillip Lougherff750312011-02-28 15:31:46 +000030 __le32 dictionary_size;
31 __le32 flags;
32};
33
Phillip Lougher9508c6b2013-11-13 02:56:26 +000034struct comp_opts {
35 int dict_size;
36};
37
38static void *squashfs_xz_comp_opts(struct squashfs_sb_info *msblk,
39 void *buff, int len)
Phillip Lougher81bb8de2010-12-09 02:02:29 +000040{
Phillip Lougher9508c6b2013-11-13 02:56:26 +000041 struct disk_comp_opts *comp_opts = buff;
42 struct comp_opts *opts;
43 int err = 0, n;
44
45 opts = kmalloc(sizeof(*opts), GFP_KERNEL);
46 if (opts == NULL) {
47 err = -ENOMEM;
48 goto out2;
49 }
Phillip Lougher81bb8de2010-12-09 02:02:29 +000050
Phillip Lougherff750312011-02-28 15:31:46 +000051 if (comp_opts) {
52 /* check compressor options are the expected length */
53 if (len < sizeof(*comp_opts)) {
54 err = -EIO;
Phillip Lougher9508c6b2013-11-13 02:56:26 +000055 goto out;
Phillip Lougherff750312011-02-28 15:31:46 +000056 }
Phillip Lougher81bb8de2010-12-09 02:02:29 +000057
Phillip Lougher9508c6b2013-11-13 02:56:26 +000058 opts->dict_size = le32_to_cpu(comp_opts->dictionary_size);
Phillip Lougherff750312011-02-28 15:31:46 +000059
60 /* the dictionary size should be 2^n or 2^n+2^(n+1) */
Phillip Lougher9508c6b2013-11-13 02:56:26 +000061 n = ffs(opts->dict_size) - 1;
62 if (opts->dict_size != (1 << n) && opts->dict_size != (1 << n) +
Phillip Lougherff750312011-02-28 15:31:46 +000063 (1 << (n + 1))) {
64 err = -EIO;
Phillip Lougher9508c6b2013-11-13 02:56:26 +000065 goto out;
Phillip Lougherff750312011-02-28 15:31:46 +000066 }
Phillip Lougher9508c6b2013-11-13 02:56:26 +000067 } else
68 /* use defaults */
69 opts->dict_size = max_t(int, msblk->block_size,
70 SQUASHFS_METADATA_SIZE);
Phillip Lougherff750312011-02-28 15:31:46 +000071
Phillip Lougher9508c6b2013-11-13 02:56:26 +000072 return opts;
73
74out:
75 kfree(opts);
76out2:
77 return ERR_PTR(err);
78}
79
80
81static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff)
82{
83 struct comp_opts *comp_opts = buff;
84 struct squashfs_xz *stream;
85 int err;
Phillip Lougherff750312011-02-28 15:31:46 +000086
87 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
88 if (stream == NULL) {
89 err = -ENOMEM;
Phillip Lougher81bb8de2010-12-09 02:02:29 +000090 goto failed;
Phillip Lougherff750312011-02-28 15:31:46 +000091 }
92
Phillip Lougher9508c6b2013-11-13 02:56:26 +000093 stream->state = xz_dec_init(XZ_PREALLOC, comp_opts->dict_size);
Phillip Lougherff750312011-02-28 15:31:46 +000094 if (stream->state == NULL) {
95 kfree(stream);
96 err = -ENOMEM;
97 goto failed;
98 }
Phillip Lougher81bb8de2010-12-09 02:02:29 +000099
100 return stream;
101
102failed:
Phillip Lougherff750312011-02-28 15:31:46 +0000103 ERROR("Failed to initialise xz decompressor\n");
104 return ERR_PTR(err);
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000105}
106
107
108static void squashfs_xz_free(void *strm)
109{
110 struct squashfs_xz *stream = strm;
111
112 if (stream) {
113 xz_dec_end(stream->state);
114 kfree(stream);
115 }
116}
117
118
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000119static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
Philippe Liard93e72b32020-06-01 21:45:23 -0700120 struct bio *bio, int offset, int length,
Phillip Lougher846b7302013-11-18 02:59:12 +0000121 struct squashfs_page_actor *output)
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000122{
Philippe Liard93e72b32020-06-01 21:45:23 -0700123 struct bvec_iter_all iter_all = {};
124 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
125 int total = 0, error = 0;
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000126 struct squashfs_xz *stream = strm;
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000127
128 xz_dec_reset(stream->state);
129 stream->buf.in_pos = 0;
130 stream->buf.in_size = 0;
131 stream->buf.out_pos = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300132 stream->buf.out_size = PAGE_SIZE;
Phillip Lougher846b7302013-11-18 02:59:12 +0000133 stream->buf.out = squashfs_first_page(output);
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000134
Philippe Liard93e72b32020-06-01 21:45:23 -0700135 for (;;) {
136 enum xz_ret xz_err;
137
138 if (stream->buf.in_pos == stream->buf.in_size) {
139 const void *data;
140 int avail;
141
142 if (!bio_next_segment(bio, &iter_all)) {
143 /* XZ_STREAM_END must be reached. */
144 error = -EIO;
145 break;
146 }
147
148 avail = min(length, ((int)bvec->bv_len) - offset);
Christoph Hellwigfbc272412021-08-04 11:56:25 +0200149 data = bvec_virt(bvec);
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000150 length -= avail;
Philippe Liard93e72b32020-06-01 21:45:23 -0700151 stream->buf.in = data + offset;
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000152 stream->buf.in_size = avail;
153 stream->buf.in_pos = 0;
154 offset = 0;
155 }
156
Phillip Lougher846b7302013-11-18 02:59:12 +0000157 if (stream->buf.out_pos == stream->buf.out_size) {
158 stream->buf.out = squashfs_next_page(output);
159 if (stream->buf.out != NULL) {
160 stream->buf.out_pos = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300161 total += PAGE_SIZE;
Phillip Lougher846b7302013-11-18 02:59:12 +0000162 }
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000163 }
164
165 xz_err = xz_dec_run(stream->state, &stream->buf);
Philippe Liard93e72b32020-06-01 21:45:23 -0700166 if (xz_err == XZ_STREAM_END)
167 break;
168 if (xz_err != XZ_OK) {
169 error = -EIO;
170 break;
171 }
172 }
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000173
Phillip Lougher846b7302013-11-18 02:59:12 +0000174 squashfs_finish_page(output);
175
Philippe Liard93e72b32020-06-01 21:45:23 -0700176 return error ? error : total + stream->buf.out_pos;
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000177}
178
179const struct squashfs_decompressor squashfs_xz_comp_ops = {
180 .init = squashfs_xz_init,
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000181 .comp_opts = squashfs_xz_comp_opts,
Phillip Lougher81bb8de2010-12-09 02:02:29 +0000182 .free = squashfs_xz_free,
183 .decompress = squashfs_xz_uncompress,
184 .id = XZ_COMPRESSION,
185 .name = "xz",
186 .supported = 1
187};