blob: d57bef91ab0852a94457a29fdaf408a1fffa1027 [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +01002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +01007 *
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +01008 * decompressor.c
9 */
10
11#include <linux/types.h>
12#include <linux/mutex.h>
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000013#include <linux/slab.h>
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010014#include <linux/buffer_head.h>
15
16#include "squashfs_fs.h"
17#include "squashfs_fs_sb.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010018#include "decompressor.h"
19#include "squashfs.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000020#include "page_actor.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010021
22/*
23 * This file (and decompressor.h) implements a decompressor framework for
24 * Squashfs, allowing multiple decompressors to be easily supported
25 */
26
Phillip Lougherdc325672009-10-14 03:58:11 +010027static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = {
Phillip Lougher9508c6b2013-11-13 02:56:26 +000028 NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
Phillip Lougherdc325672009-10-14 03:58:11 +010029};
30
Phillip Lougher62421642014-11-27 18:48:44 +000031#ifndef CONFIG_SQUASHFS_LZ4
32static const struct squashfs_decompressor squashfs_lz4_comp_ops = {
33 NULL, NULL, NULL, NULL, LZ4_COMPRESSION, "lz4", 0
34};
35#endif
36
Chan Jeong79cb8ce2010-08-05 02:29:59 +010037#ifndef CONFIG_SQUASHFS_LZO
Phillip Lougher01a678c2011-01-05 18:23:53 +000038static const struct squashfs_decompressor squashfs_lzo_comp_ops = {
Phillip Lougher9508c6b2013-11-13 02:56:26 +000039 NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
Phillip Lougherdc325672009-10-14 03:58:11 +010040};
Chan Jeong79cb8ce2010-08-05 02:29:59 +010041#endif
Phillip Lougherdc325672009-10-14 03:58:11 +010042
Phillip Lougher7a43ae52010-12-09 02:08:31 +000043#ifndef CONFIG_SQUASHFS_XZ
44static const struct squashfs_decompressor squashfs_xz_comp_ops = {
Phillip Lougher9508c6b2013-11-13 02:56:26 +000045 NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
Phillip Lougher7a43ae52010-12-09 02:08:31 +000046};
47#endif
48
Phillip Loughercc6d3492011-07-22 03:01:28 +010049#ifndef CONFIG_SQUASHFS_ZLIB
50static const struct squashfs_decompressor squashfs_zlib_comp_ops = {
Phillip Lougher9508c6b2013-11-13 02:56:26 +000051 NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
Phillip Loughercc6d3492011-07-22 03:01:28 +010052};
53#endif
54
Sean Purcell87bf54b2017-08-09 19:42:36 -070055#ifndef CONFIG_SQUASHFS_ZSTD
56static const struct squashfs_decompressor squashfs_zstd_comp_ops = {
57 NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0
58};
59#endif
60
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010061static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
Phillip Lougher9508c6b2013-11-13 02:56:26 +000062 NULL, NULL, NULL, NULL, 0, "unknown", 0
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010063};
64
65static const struct squashfs_decompressor *decompressor[] = {
66 &squashfs_zlib_comp_ops,
Phillip Lougher62421642014-11-27 18:48:44 +000067 &squashfs_lz4_comp_ops,
Chan Jeong79cb8ce2010-08-05 02:29:59 +010068 &squashfs_lzo_comp_ops,
Phillip Lougher7a43ae52010-12-09 02:08:31 +000069 &squashfs_xz_comp_ops,
Phillip Lougher01a678c2011-01-05 18:23:53 +000070 &squashfs_lzma_unsupported_comp_ops,
Sean Purcell87bf54b2017-08-09 19:42:36 -070071 &squashfs_zstd_comp_ops,
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010072 &squashfs_unknown_comp_ops
73};
74
75
76const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
77{
78 int i;
79
80 for (i = 0; decompressor[i]->id; i++)
81 if (id == decompressor[i]->id)
82 break;
83
84 return decompressor[i];
85}
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000086
87
Phillip Lougher9508c6b2013-11-13 02:56:26 +000088static void *get_comp_opts(struct super_block *sb, unsigned short flags)
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000089{
90 struct squashfs_sb_info *msblk = sb->s_fs_info;
Phillip Lougher9508c6b2013-11-13 02:56:26 +000091 void *buffer = NULL, *comp_opts;
Phillip Lougher846b7302013-11-18 02:59:12 +000092 struct squashfs_page_actor *actor = NULL;
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000093 int length = 0;
94
95 /*
96 * Read decompressor specific options from file system if present
97 */
98 if (SQUASHFS_COMP_OPTS(flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000100 if (buffer == NULL) {
101 comp_opts = ERR_PTR(-ENOMEM);
102 goto out;
103 }
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000104
Phillip Lougher846b7302013-11-18 02:59:12 +0000105 actor = squashfs_page_actor_init(&buffer, 1, 0);
106 if (actor == NULL) {
107 comp_opts = ERR_PTR(-ENOMEM);
108 goto out;
109 }
110
111 length = squashfs_read_data(sb,
112 sizeof(struct squashfs_super_block), 0, NULL, actor);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000113
114 if (length < 0) {
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000115 comp_opts = ERR_PTR(length);
116 goto out;
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000117 }
118 }
119
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000120 comp_opts = squashfs_comp_opts(msblk, buffer, length);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000121
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000122out:
Phillip Lougher846b7302013-11-18 02:59:12 +0000123 kfree(actor);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000124 kfree(buffer);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000125 return comp_opts;
126}
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000127
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000128
129void *squashfs_decompressor_setup(struct super_block *sb, unsigned short flags)
130{
131 struct squashfs_sb_info *msblk = sb->s_fs_info;
132 void *stream, *comp_opts = get_comp_opts(sb, flags);
133
134 if (IS_ERR(comp_opts))
135 return comp_opts;
136
137 stream = squashfs_decompressor_create(msblk, comp_opts);
138 if (IS_ERR(stream))
139 kfree(comp_opts);
140
141 return stream;
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +0000142}