0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/crypto.h>
0019 #include "ubifs.h"
0020
0021
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
0081 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
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
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
0128
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
0143
0144
0145
0146
0147
0148
0149
0150
0151
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
0192
0193
0194
0195
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
0214
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
0225
0226
0227
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
0257
0258 void ubifs_compressors_exit(void)
0259 {
0260 compr_exit(&lzo_compr);
0261 compr_exit(&zlib_compr);
0262 compr_exit(&zstd_compr);
0263 }