1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Squashfs - a compressed read only filesystem for Linux
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6*4882a593Smuzhiyun * Phillip Lougher <phillip@squashfs.org.uk>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * decompressor.c
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun #include <linux/mutex.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/buffer_head.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include "squashfs_fs.h"
17*4882a593Smuzhiyun #include "squashfs_fs_sb.h"
18*4882a593Smuzhiyun #include "decompressor.h"
19*4882a593Smuzhiyun #include "squashfs.h"
20*4882a593Smuzhiyun #include "page_actor.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * This file (and decompressor.h) implements a decompressor framework for
24*4882a593Smuzhiyun * Squashfs, allowing multiple decompressors to be easily supported
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = {
28*4882a593Smuzhiyun NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifndef CONFIG_SQUASHFS_LZ4
32*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_lz4_comp_ops = {
33*4882a593Smuzhiyun NULL, NULL, NULL, NULL, LZ4_COMPRESSION, "lz4", 0
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #ifndef CONFIG_SQUASHFS_LZO
38*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_lzo_comp_ops = {
39*4882a593Smuzhiyun NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #ifndef CONFIG_SQUASHFS_XZ
44*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_xz_comp_ops = {
45*4882a593Smuzhiyun NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #ifndef CONFIG_SQUASHFS_ZLIB
50*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_zlib_comp_ops = {
51*4882a593Smuzhiyun NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #ifndef CONFIG_SQUASHFS_ZSTD
56*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_zstd_comp_ops = {
57*4882a593Smuzhiyun NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun #endif
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
62*4882a593Smuzhiyun NULL, NULL, NULL, NULL, 0, "unknown", 0
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun static const struct squashfs_decompressor *decompressor[] = {
66*4882a593Smuzhiyun &squashfs_zlib_comp_ops,
67*4882a593Smuzhiyun &squashfs_lz4_comp_ops,
68*4882a593Smuzhiyun &squashfs_lzo_comp_ops,
69*4882a593Smuzhiyun &squashfs_xz_comp_ops,
70*4882a593Smuzhiyun &squashfs_lzma_unsupported_comp_ops,
71*4882a593Smuzhiyun &squashfs_zstd_comp_ops,
72*4882a593Smuzhiyun &squashfs_unknown_comp_ops
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun
squashfs_lookup_decompressor(int id)76*4882a593Smuzhiyun const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun int i;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun for (i = 0; decompressor[i]->id; i++)
81*4882a593Smuzhiyun if (id == decompressor[i]->id)
82*4882a593Smuzhiyun break;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun return decompressor[i];
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun
get_comp_opts(struct super_block * sb,unsigned short flags)88*4882a593Smuzhiyun static void *get_comp_opts(struct super_block *sb, unsigned short flags)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun struct squashfs_sb_info *msblk = sb->s_fs_info;
91*4882a593Smuzhiyun void *buffer = NULL, *comp_opts;
92*4882a593Smuzhiyun struct squashfs_page_actor *actor = NULL;
93*4882a593Smuzhiyun int length = 0;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * Read decompressor specific options from file system if present
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun if (SQUASHFS_COMP_OPTS(flags)) {
99*4882a593Smuzhiyun buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
100*4882a593Smuzhiyun if (buffer == NULL) {
101*4882a593Smuzhiyun comp_opts = ERR_PTR(-ENOMEM);
102*4882a593Smuzhiyun goto out;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun actor = squashfs_page_actor_init(&buffer, 1, 0);
106*4882a593Smuzhiyun if (actor == NULL) {
107*4882a593Smuzhiyun comp_opts = ERR_PTR(-ENOMEM);
108*4882a593Smuzhiyun goto out;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun length = squashfs_read_data(sb,
112*4882a593Smuzhiyun sizeof(struct squashfs_super_block), 0, NULL, actor);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (length < 0) {
115*4882a593Smuzhiyun comp_opts = ERR_PTR(length);
116*4882a593Smuzhiyun goto out;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun comp_opts = squashfs_comp_opts(msblk, buffer, length);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun out:
123*4882a593Smuzhiyun kfree(actor);
124*4882a593Smuzhiyun kfree(buffer);
125*4882a593Smuzhiyun return comp_opts;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun
squashfs_decompressor_setup(struct super_block * sb,unsigned short flags)129*4882a593Smuzhiyun void *squashfs_decompressor_setup(struct super_block *sb, unsigned short flags)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct squashfs_sb_info *msblk = sb->s_fs_info;
132*4882a593Smuzhiyun void *stream, *comp_opts = get_comp_opts(sb, flags);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (IS_ERR(comp_opts))
135*4882a593Smuzhiyun return comp_opts;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun stream = squashfs_decompressor_create(msblk, comp_opts);
138*4882a593Smuzhiyun if (IS_ERR(stream))
139*4882a593Smuzhiyun kfree(comp_opts);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun return stream;
142*4882a593Smuzhiyun }
143