Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  * Copyright (C) 2006, 2007 University of Szeged, Hungary
0007  *
0008  * Authors: Adrian Hunter
0009  *          Artem Bityutskiy (Битюцкий Артём)
0010  *          Zoltan Sogor
0011  */
0012 
0013 /*
0014  * This file provides a single place to access to compression and
0015  * decompression.
0016  */
0017 
0018 #include <linux/crypto.h>
0019 #include "ubifs.h"
0020 
0021 /* Fake description object for the "none" compressor */
0022 static struct ubifs_compressor none_compr = {
0023     .compr_type = UBIFS_COMPR_NONE,
0024     .name = "none",
0025     .capi_name = "",
0026 };
0027 
0028 #ifdef CONFIG_UBIFS_FS_LZO
0029 static DEFINE_MUTEX(lzo_mutex);
0030 
0031 static struct ubifs_compressor lzo_compr = {
0032     .compr_type = UBIFS_COMPR_LZO,
0033     .comp_mutex = &lzo_mutex,
0034     .name = "lzo",
0035     .capi_name = "lzo",
0036 };
0037 #else
0038 static struct ubifs_compressor lzo_compr = {
0039     .compr_type = UBIFS_COMPR_LZO,
0040     .name = "lzo",
0041 };
0042 #endif
0043 
0044 #ifdef CONFIG_UBIFS_FS_ZLIB
0045 static DEFINE_MUTEX(deflate_mutex);
0046 static DEFINE_MUTEX(inflate_mutex);
0047 
0048 static struct ubifs_compressor zlib_compr = {
0049     .compr_type = UBIFS_COMPR_ZLIB,
0050     .comp_mutex = &deflate_mutex,
0051     .decomp_mutex = &inflate_mutex,
0052     .name = "zlib",
0053     .capi_name = "deflate",
0054 };
0055 #else
0056 static struct ubifs_compressor zlib_compr = {
0057     .compr_type = UBIFS_COMPR_ZLIB,
0058     .name = "zlib",
0059 };
0060 #endif
0061 
0062 #ifdef CONFIG_UBIFS_FS_ZSTD
0063 static DEFINE_MUTEX(zstd_enc_mutex);
0064 static DEFINE_MUTEX(zstd_dec_mutex);
0065 
0066 static struct ubifs_compressor zstd_compr = {
0067     .compr_type = UBIFS_COMPR_ZSTD,
0068     .comp_mutex = &zstd_enc_mutex,
0069     .decomp_mutex = &zstd_dec_mutex,
0070     .name = "zstd",
0071     .capi_name = "zstd",
0072 };
0073 #else
0074 static struct ubifs_compressor zstd_compr = {
0075     .compr_type = UBIFS_COMPR_ZSTD,
0076     .name = "zstd",
0077 };
0078 #endif
0079 
0080 /* All UBIFS compressors */
0081 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
0082 
0083 /**
0084  * ubifs_compress - compress data.
0085  * @in_buf: data to compress
0086  * @in_len: length of the data to compress
0087  * @out_buf: output buffer where compressed data should be stored
0088  * @out_len: output buffer length is returned here
0089  * @compr_type: type of compression to use on enter, actually used compression
0090  *              type on exit
0091  *
0092  * This function compresses input buffer @in_buf of length @in_len and stores
0093  * the result in the output buffer @out_buf and the resulting length in
0094  * @out_len. If the input buffer does not compress, it is just copied to the
0095  * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if
0096  * compression error occurred.
0097  *
0098  * Note, if the input buffer was not compressed, it is copied to the output
0099  * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
0100  */
0101 void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
0102             int in_len, void *out_buf, int *out_len, int *compr_type)
0103 {
0104     int err;
0105     struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
0106 
0107     if (*compr_type == UBIFS_COMPR_NONE)
0108         goto no_compr;
0109 
0110     /* If the input data is small, do not even try to compress it */
0111     if (in_len < UBIFS_MIN_COMPR_LEN)
0112         goto no_compr;
0113 
0114     if (compr->comp_mutex)
0115         mutex_lock(compr->comp_mutex);
0116     err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
0117                    (unsigned int *)out_len);
0118     if (compr->comp_mutex)
0119         mutex_unlock(compr->comp_mutex);
0120     if (unlikely(err)) {
0121         ubifs_warn(c, "cannot compress %d bytes, compressor %s, error %d, leave data uncompressed",
0122                in_len, compr->name, err);
0123         goto no_compr;
0124     }
0125 
0126     /*
0127      * If the data compressed only slightly, it is better to leave it
0128      * uncompressed to improve read speed.
0129      */
0130     if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
0131         goto no_compr;
0132 
0133     return;
0134 
0135 no_compr:
0136     memcpy(out_buf, in_buf, in_len);
0137     *out_len = in_len;
0138     *compr_type = UBIFS_COMPR_NONE;
0139 }
0140 
0141 /**
0142  * ubifs_decompress - decompress data.
0143  * @in_buf: data to decompress
0144  * @in_len: length of the data to decompress
0145  * @out_buf: output buffer where decompressed data should
0146  * @out_len: output length is returned here
0147  * @compr_type: type of compression
0148  *
0149  * This function decompresses data from buffer @in_buf into buffer @out_buf.
0150  * The length of the uncompressed data is returned in @out_len. This functions
0151  * returns %0 on success or a negative error code on failure.
0152  */
0153 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
0154              int in_len, void *out_buf, int *out_len, int compr_type)
0155 {
0156     int err;
0157     struct ubifs_compressor *compr;
0158 
0159     if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
0160         ubifs_err(c, "invalid compression type %d", compr_type);
0161         return -EINVAL;
0162     }
0163 
0164     compr = ubifs_compressors[compr_type];
0165 
0166     if (unlikely(!compr->capi_name)) {
0167         ubifs_err(c, "%s compression is not compiled in", compr->name);
0168         return -EINVAL;
0169     }
0170 
0171     if (compr_type == UBIFS_COMPR_NONE) {
0172         memcpy(out_buf, in_buf, in_len);
0173         *out_len = in_len;
0174         return 0;
0175     }
0176 
0177     if (compr->decomp_mutex)
0178         mutex_lock(compr->decomp_mutex);
0179     err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
0180                      (unsigned int *)out_len);
0181     if (compr->decomp_mutex)
0182         mutex_unlock(compr->decomp_mutex);
0183     if (err)
0184         ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
0185               in_len, compr->name, err);
0186 
0187     return err;
0188 }
0189 
0190 /**
0191  * compr_init - initialize a compressor.
0192  * @compr: compressor description object
0193  *
0194  * This function initializes the requested compressor and returns zero in case
0195  * of success or a negative error code in case of failure.
0196  */
0197 static int __init compr_init(struct ubifs_compressor *compr)
0198 {
0199     if (compr->capi_name) {
0200         compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
0201         if (IS_ERR(compr->cc)) {
0202             pr_err("UBIFS error (pid %d): cannot initialize compressor %s, error %ld",
0203                    current->pid, compr->name, PTR_ERR(compr->cc));
0204             return PTR_ERR(compr->cc);
0205         }
0206     }
0207 
0208     ubifs_compressors[compr->compr_type] = compr;
0209     return 0;
0210 }
0211 
0212 /**
0213  * compr_exit - de-initialize a compressor.
0214  * @compr: compressor description object
0215  */
0216 static void compr_exit(struct ubifs_compressor *compr)
0217 {
0218     if (compr->capi_name)
0219         crypto_free_comp(compr->cc);
0220     return;
0221 }
0222 
0223 /**
0224  * ubifs_compressors_init - initialize UBIFS compressors.
0225  *
0226  * This function initializes the compressor which were compiled in. Returns
0227  * zero in case of success and a negative error code in case of failure.
0228  */
0229 int __init ubifs_compressors_init(void)
0230 {
0231     int err;
0232 
0233     err = compr_init(&lzo_compr);
0234     if (err)
0235         return err;
0236 
0237     err = compr_init(&zstd_compr);
0238     if (err)
0239         goto out_lzo;
0240 
0241     err = compr_init(&zlib_compr);
0242     if (err)
0243         goto out_zstd;
0244 
0245     ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
0246     return 0;
0247 
0248 out_zstd:
0249     compr_exit(&zstd_compr);
0250 out_lzo:
0251     compr_exit(&lzo_compr);
0252     return err;
0253 }
0254 
0255 /**
0256  * ubifs_compressors_exit - de-initialize UBIFS compressors.
0257  */
0258 void ubifs_compressors_exit(void)
0259 {
0260     compr_exit(&lzo_compr);
0261     compr_exit(&zlib_compr);
0262     compr_exit(&zstd_compr);
0263 }