Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Squashfs - a compressed read only filesystem for Linux
0004  *
0005  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
0006  * Phillip Lougher <phillip@squashfs.org.uk>
0007  *
0008  * decompressor.c
0009  */
0010 
0011 #include <linux/types.h>
0012 #include <linux/mutex.h>
0013 #include <linux/slab.h>
0014 #include <linux/buffer_head.h>
0015 
0016 #include "squashfs_fs.h"
0017 #include "squashfs_fs_sb.h"
0018 #include "decompressor.h"
0019 #include "squashfs.h"
0020 #include "page_actor.h"
0021 
0022 /*
0023  * This file (and decompressor.h) implements a decompressor framework for
0024  * Squashfs, allowing multiple decompressors to be easily supported
0025  */
0026 
0027 static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = {
0028     NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
0029 };
0030 
0031 #ifndef CONFIG_SQUASHFS_LZ4
0032 static const struct squashfs_decompressor squashfs_lz4_comp_ops = {
0033     NULL, NULL, NULL, NULL, LZ4_COMPRESSION, "lz4", 0
0034 };
0035 #endif
0036 
0037 #ifndef CONFIG_SQUASHFS_LZO
0038 static const struct squashfs_decompressor squashfs_lzo_comp_ops = {
0039     NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
0040 };
0041 #endif
0042 
0043 #ifndef CONFIG_SQUASHFS_XZ
0044 static const struct squashfs_decompressor squashfs_xz_comp_ops = {
0045     NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
0046 };
0047 #endif
0048 
0049 #ifndef CONFIG_SQUASHFS_ZLIB
0050 static const struct squashfs_decompressor squashfs_zlib_comp_ops = {
0051     NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
0052 };
0053 #endif
0054 
0055 #ifndef CONFIG_SQUASHFS_ZSTD
0056 static const struct squashfs_decompressor squashfs_zstd_comp_ops = {
0057     NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0
0058 };
0059 #endif
0060 
0061 static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
0062     NULL, NULL, NULL, NULL, 0, "unknown", 0
0063 };
0064 
0065 static const struct squashfs_decompressor *decompressor[] = {
0066     &squashfs_zlib_comp_ops,
0067     &squashfs_lz4_comp_ops,
0068     &squashfs_lzo_comp_ops,
0069     &squashfs_xz_comp_ops,
0070     &squashfs_lzma_unsupported_comp_ops,
0071     &squashfs_zstd_comp_ops,
0072     &squashfs_unknown_comp_ops
0073 };
0074 
0075 
0076 const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
0077 {
0078     int i;
0079 
0080     for (i = 0; decompressor[i]->id; i++)
0081         if (id == decompressor[i]->id)
0082             break;
0083 
0084     return decompressor[i];
0085 }
0086 
0087 
0088 static void *get_comp_opts(struct super_block *sb, unsigned short flags)
0089 {
0090     struct squashfs_sb_info *msblk = sb->s_fs_info;
0091     void *buffer = NULL, *comp_opts;
0092     struct squashfs_page_actor *actor = NULL;
0093     int length = 0;
0094 
0095     /*
0096      * Read decompressor specific options from file system if present
0097      */
0098     if (SQUASHFS_COMP_OPTS(flags)) {
0099         buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
0100         if (buffer == NULL) {
0101             comp_opts = ERR_PTR(-ENOMEM);
0102             goto out;
0103         }
0104 
0105         actor = squashfs_page_actor_init(&buffer, 1, 0);
0106         if (actor == NULL) {
0107             comp_opts = ERR_PTR(-ENOMEM);
0108             goto out;
0109         }
0110 
0111         length = squashfs_read_data(sb,
0112             sizeof(struct squashfs_super_block), 0, NULL, actor);
0113 
0114         if (length < 0) {
0115             comp_opts = ERR_PTR(length);
0116             goto out;
0117         }
0118     }
0119 
0120     comp_opts = squashfs_comp_opts(msblk, buffer, length);
0121 
0122 out:
0123     kfree(actor);
0124     kfree(buffer);
0125     return comp_opts;
0126 }
0127 
0128 
0129 void *squashfs_decompressor_setup(struct super_block *sb, unsigned short flags)
0130 {
0131     struct squashfs_sb_info *msblk = sb->s_fs_info;
0132     void *stream, *comp_opts = get_comp_opts(sb, flags);
0133 
0134     if (IS_ERR(comp_opts))
0135         return comp_opts;
0136 
0137     stream = squashfs_decompressor_create(msblk, comp_opts);
0138     if (IS_ERR(stream))
0139         kfree(comp_opts);
0140 
0141     return stream;
0142 }