1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
6 * Phillip Lougher <phillip@squashfs.org.uk>
12 #include <linux/mutex.h>
13 #include <linux/bio.h>
14 #include <linux/slab.h>
16 #include <linux/bitops.h>
18 #include "squashfs_fs.h"
19 #include "squashfs_fs_sb.h"
21 #include "decompressor.h"
22 #include "page_actor.h"
29 struct disk_comp_opts
{
30 __le32 dictionary_size
;
38 static void *squashfs_xz_comp_opts(struct squashfs_sb_info
*msblk
,
41 struct disk_comp_opts
*comp_opts
= buff
;
42 struct comp_opts
*opts
;
45 opts
= kmalloc(sizeof(*opts
), GFP_KERNEL
);
52 /* check compressor options are the expected length */
53 if (len
< sizeof(*comp_opts
)) {
58 opts
->dict_size
= le32_to_cpu(comp_opts
->dictionary_size
);
60 /* the dictionary size should be 2^n or 2^n+2^(n+1) */
61 n
= ffs(opts
->dict_size
) - 1;
62 if (opts
->dict_size
!= (1 << n
) && opts
->dict_size
!= (1 << n
) +
69 opts
->dict_size
= max_t(int, msblk
->block_size
,
70 SQUASHFS_METADATA_SIZE
);
81 static void *squashfs_xz_init(struct squashfs_sb_info
*msblk
, void *buff
)
83 struct comp_opts
*comp_opts
= buff
;
84 struct squashfs_xz
*stream
;
87 stream
= kmalloc(sizeof(*stream
), GFP_KERNEL
);
93 stream
->state
= xz_dec_init(XZ_PREALLOC
, comp_opts
->dict_size
);
94 if (stream
->state
== NULL
) {
103 ERROR("Failed to initialise xz decompressor\n");
108 static void squashfs_xz_free(void *strm
)
110 struct squashfs_xz
*stream
= strm
;
113 xz_dec_end(stream
->state
);
119 static int squashfs_xz_uncompress(struct squashfs_sb_info
*msblk
, void *strm
,
120 struct bio
*bio
, int offset
, int length
,
121 struct squashfs_page_actor
*output
)
123 struct bvec_iter_all iter_all
= {};
124 struct bio_vec
*bvec
= bvec_init_iter_all(&iter_all
);
125 int total
= 0, error
= 0;
126 struct squashfs_xz
*stream
= strm
;
128 xz_dec_reset(stream
->state
);
129 stream
->buf
.in_pos
= 0;
130 stream
->buf
.in_size
= 0;
131 stream
->buf
.out_pos
= 0;
132 stream
->buf
.out_size
= PAGE_SIZE
;
133 stream
->buf
.out
= squashfs_first_page(output
);
138 if (stream
->buf
.in_pos
== stream
->buf
.in_size
) {
142 if (!bio_next_segment(bio
, &iter_all
)) {
143 /* XZ_STREAM_END must be reached. */
148 avail
= min(length
, ((int)bvec
->bv_len
) - offset
);
149 data
= page_address(bvec
->bv_page
) + bvec
->bv_offset
;
151 stream
->buf
.in
= data
+ offset
;
152 stream
->buf
.in_size
= avail
;
153 stream
->buf
.in_pos
= 0;
157 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;
165 xz_err
= xz_dec_run(stream
->state
, &stream
->buf
);
166 if (xz_err
== XZ_STREAM_END
)
168 if (xz_err
!= XZ_OK
) {
174 squashfs_finish_page(output
);
176 return error
? error
: total
+ stream
->buf
.out_pos
;
179 const struct squashfs_decompressor squashfs_xz_comp_ops
= {
180 .init
= squashfs_xz_init
,
181 .comp_opts
= squashfs_xz_comp_opts
,
182 .free
= squashfs_xz_free
,
183 .decompress
= squashfs_xz_uncompress
,
184 .id
= XZ_COMPRESSION
,