Thomas Gleixner | 68252eb | 2019-05-20 19:08:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Squashfs - a compressed read only filesystem for Linux |
| 4 | * |
| 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 6 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 7 | * |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 8 | * decompressor.c |
| 9 | */ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/mutex.h> |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 13 | #include <linux/slab.h> |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 14 | #include <linux/buffer_head.h> |
| 15 | |
| 16 | #include "squashfs_fs.h" |
| 17 | #include "squashfs_fs_sb.h" |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 18 | #include "decompressor.h" |
| 19 | #include "squashfs.h" |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 20 | #include "page_actor.h" |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * This file (and decompressor.h) implements a decompressor framework for |
| 24 | * Squashfs, allowing multiple decompressors to be easily supported |
| 25 | */ |
| 26 | |
Phillip Lougher | dc32567 | 2009-10-14 03:58:11 +0100 | [diff] [blame] | 27 | static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 28 | NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0 |
Phillip Lougher | dc32567 | 2009-10-14 03:58:11 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
Phillip Lougher | 6242164 | 2014-11-27 18:48:44 +0000 | [diff] [blame] | 31 | #ifndef CONFIG_SQUASHFS_LZ4 |
| 32 | static const struct squashfs_decompressor squashfs_lz4_comp_ops = { |
| 33 | NULL, NULL, NULL, NULL, LZ4_COMPRESSION, "lz4", 0 |
| 34 | }; |
| 35 | #endif |
| 36 | |
Chan Jeong | 79cb8ce | 2010-08-05 02:29:59 +0100 | [diff] [blame] | 37 | #ifndef CONFIG_SQUASHFS_LZO |
Phillip Lougher | 01a678c | 2011-01-05 18:23:53 +0000 | [diff] [blame] | 38 | static const struct squashfs_decompressor squashfs_lzo_comp_ops = { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 39 | NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0 |
Phillip Lougher | dc32567 | 2009-10-14 03:58:11 +0100 | [diff] [blame] | 40 | }; |
Chan Jeong | 79cb8ce | 2010-08-05 02:29:59 +0100 | [diff] [blame] | 41 | #endif |
Phillip Lougher | dc32567 | 2009-10-14 03:58:11 +0100 | [diff] [blame] | 42 | |
Phillip Lougher | 7a43ae5 | 2010-12-09 02:08:31 +0000 | [diff] [blame] | 43 | #ifndef CONFIG_SQUASHFS_XZ |
| 44 | static const struct squashfs_decompressor squashfs_xz_comp_ops = { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 45 | NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0 |
Phillip Lougher | 7a43ae5 | 2010-12-09 02:08:31 +0000 | [diff] [blame] | 46 | }; |
| 47 | #endif |
| 48 | |
Phillip Lougher | cc6d349 | 2011-07-22 03:01:28 +0100 | [diff] [blame] | 49 | #ifndef CONFIG_SQUASHFS_ZLIB |
| 50 | static const struct squashfs_decompressor squashfs_zlib_comp_ops = { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 51 | NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0 |
Phillip Lougher | cc6d349 | 2011-07-22 03:01:28 +0100 | [diff] [blame] | 52 | }; |
| 53 | #endif |
| 54 | |
Sean Purcell | 87bf54b | 2017-08-09 19:42:36 -0700 | [diff] [blame] | 55 | #ifndef CONFIG_SQUASHFS_ZSTD |
| 56 | static const struct squashfs_decompressor squashfs_zstd_comp_ops = { |
| 57 | NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0 |
| 58 | }; |
| 59 | #endif |
| 60 | |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 61 | static const struct squashfs_decompressor squashfs_unknown_comp_ops = { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 62 | NULL, NULL, NULL, NULL, 0, "unknown", 0 |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static const struct squashfs_decompressor *decompressor[] = { |
| 66 | &squashfs_zlib_comp_ops, |
Phillip Lougher | 6242164 | 2014-11-27 18:48:44 +0000 | [diff] [blame] | 67 | &squashfs_lz4_comp_ops, |
Chan Jeong | 79cb8ce | 2010-08-05 02:29:59 +0100 | [diff] [blame] | 68 | &squashfs_lzo_comp_ops, |
Phillip Lougher | 7a43ae5 | 2010-12-09 02:08:31 +0000 | [diff] [blame] | 69 | &squashfs_xz_comp_ops, |
Phillip Lougher | 01a678c | 2011-01-05 18:23:53 +0000 | [diff] [blame] | 70 | &squashfs_lzma_unsupported_comp_ops, |
Sean Purcell | 87bf54b | 2017-08-09 19:42:36 -0700 | [diff] [blame] | 71 | &squashfs_zstd_comp_ops, |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 72 | &squashfs_unknown_comp_ops |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | const 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 Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 86 | |
| 87 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 88 | static void *get_comp_opts(struct super_block *sb, unsigned short flags) |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 89 | { |
| 90 | struct squashfs_sb_info *msblk = sb->s_fs_info; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 91 | void *buffer = NULL, *comp_opts; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 92 | struct squashfs_page_actor *actor = NULL; |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 93 | int length = 0; |
| 94 | |
| 95 | /* |
| 96 | * Read decompressor specific options from file system if present |
| 97 | */ |
| 98 | if (SQUASHFS_COMP_OPTS(flags)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 99 | buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 100 | if (buffer == NULL) { |
| 101 | comp_opts = ERR_PTR(-ENOMEM); |
| 102 | goto out; |
| 103 | } |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 104 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 105 | 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 Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 113 | |
| 114 | if (length < 0) { |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 115 | comp_opts = ERR_PTR(length); |
| 116 | goto out; |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 120 | comp_opts = squashfs_comp_opts(msblk, buffer, length); |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 121 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 122 | out: |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 123 | kfree(actor); |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 124 | kfree(buffer); |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 125 | return comp_opts; |
| 126 | } |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 127 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 128 | |
| 129 | void *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 Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 142 | } |