1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * This file is part of UBIFS. 3 * This file is part of UBIFS. 4 * 4 * 5 * Copyright (C) 2006-2008 Nokia Corporation. 5 * Copyright (C) 2006-2008 Nokia Corporation. 6 * Copyright (C) 2006, 2007 University of Szeg 6 * Copyright (C) 2006, 2007 University of Szeged, Hungary 7 * 7 * 8 * Authors: Adrian Hunter 8 * Authors: Adrian Hunter 9 * Artem Bityutskiy (Битюцкий 9 * Artem Bityutskiy (Битюцкий Артём) 10 * Zoltan Sogor 10 * Zoltan Sogor 11 */ 11 */ 12 12 13 /* 13 /* 14 * This file provides a single place to access 14 * This file provides a single place to access to compression and 15 * decompression. 15 * decompression. 16 */ 16 */ 17 17 18 #include <linux/crypto.h> 18 #include <linux/crypto.h> 19 #include "ubifs.h" 19 #include "ubifs.h" 20 20 21 /* Fake description object for the "none" comp 21 /* Fake description object for the "none" compressor */ 22 static struct ubifs_compressor none_compr = { 22 static struct ubifs_compressor none_compr = { 23 .compr_type = UBIFS_COMPR_NONE, 23 .compr_type = UBIFS_COMPR_NONE, 24 .name = "none", 24 .name = "none", 25 .capi_name = "", 25 .capi_name = "", 26 }; 26 }; 27 27 28 #ifdef CONFIG_UBIFS_FS_LZO 28 #ifdef CONFIG_UBIFS_FS_LZO 29 static DEFINE_MUTEX(lzo_mutex); 29 static DEFINE_MUTEX(lzo_mutex); 30 30 31 static struct ubifs_compressor lzo_compr = { 31 static struct ubifs_compressor lzo_compr = { 32 .compr_type = UBIFS_COMPR_LZO, 32 .compr_type = UBIFS_COMPR_LZO, 33 .comp_mutex = &lzo_mutex, 33 .comp_mutex = &lzo_mutex, 34 .name = "lzo", 34 .name = "lzo", 35 .capi_name = "lzo", 35 .capi_name = "lzo", 36 }; 36 }; 37 #else 37 #else 38 static struct ubifs_compressor lzo_compr = { 38 static struct ubifs_compressor lzo_compr = { 39 .compr_type = UBIFS_COMPR_LZO, 39 .compr_type = UBIFS_COMPR_LZO, 40 .name = "lzo", 40 .name = "lzo", 41 }; 41 }; 42 #endif 42 #endif 43 43 44 #ifdef CONFIG_UBIFS_FS_ZLIB 44 #ifdef CONFIG_UBIFS_FS_ZLIB 45 static DEFINE_MUTEX(deflate_mutex); 45 static DEFINE_MUTEX(deflate_mutex); 46 static DEFINE_MUTEX(inflate_mutex); 46 static DEFINE_MUTEX(inflate_mutex); 47 47 48 static struct ubifs_compressor zlib_compr = { 48 static struct ubifs_compressor zlib_compr = { 49 .compr_type = UBIFS_COMPR_ZLIB, 49 .compr_type = UBIFS_COMPR_ZLIB, 50 .comp_mutex = &deflate_mutex, 50 .comp_mutex = &deflate_mutex, 51 .decomp_mutex = &inflate_mutex, 51 .decomp_mutex = &inflate_mutex, 52 .name = "zlib", 52 .name = "zlib", 53 .capi_name = "deflate", 53 .capi_name = "deflate", 54 }; 54 }; 55 #else 55 #else 56 static struct ubifs_compressor zlib_compr = { 56 static struct ubifs_compressor zlib_compr = { 57 .compr_type = UBIFS_COMPR_ZLIB, 57 .compr_type = UBIFS_COMPR_ZLIB, 58 .name = "zlib", 58 .name = "zlib", 59 }; 59 }; 60 #endif 60 #endif 61 61 62 #ifdef CONFIG_UBIFS_FS_ZSTD 62 #ifdef CONFIG_UBIFS_FS_ZSTD 63 static DEFINE_MUTEX(zstd_enc_mutex); 63 static DEFINE_MUTEX(zstd_enc_mutex); 64 static DEFINE_MUTEX(zstd_dec_mutex); 64 static DEFINE_MUTEX(zstd_dec_mutex); 65 65 66 static struct ubifs_compressor zstd_compr = { 66 static struct ubifs_compressor zstd_compr = { 67 .compr_type = UBIFS_COMPR_ZSTD, 67 .compr_type = UBIFS_COMPR_ZSTD, 68 .comp_mutex = &zstd_enc_mutex, 68 .comp_mutex = &zstd_enc_mutex, 69 .decomp_mutex = &zstd_dec_mutex, 69 .decomp_mutex = &zstd_dec_mutex, 70 .name = "zstd", 70 .name = "zstd", 71 .capi_name = "zstd", 71 .capi_name = "zstd", 72 }; 72 }; 73 #else 73 #else 74 static struct ubifs_compressor zstd_compr = { 74 static struct ubifs_compressor zstd_compr = { 75 .compr_type = UBIFS_COMPR_ZSTD, 75 .compr_type = UBIFS_COMPR_ZSTD, 76 .name = "zstd", 76 .name = "zstd", 77 }; 77 }; 78 #endif 78 #endif 79 79 80 /* All UBIFS compressors */ 80 /* All UBIFS compressors */ 81 struct ubifs_compressor *ubifs_compressors[UBI 81 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT]; 82 82 83 /** 83 /** 84 * ubifs_compress - compress data. 84 * ubifs_compress - compress data. 85 * @c: UBIFS file-system description object << 86 * @in_buf: data to compress 85 * @in_buf: data to compress 87 * @in_len: length of the data to compress 86 * @in_len: length of the data to compress 88 * @out_buf: output buffer where compressed da 87 * @out_buf: output buffer where compressed data should be stored 89 * @out_len: output buffer length is returned 88 * @out_len: output buffer length is returned here 90 * @compr_type: type of compression to use on 89 * @compr_type: type of compression to use on enter, actually used compression 91 * type on exit 90 * type on exit 92 * 91 * 93 * This function compresses input buffer @in_b 92 * This function compresses input buffer @in_buf of length @in_len and stores 94 * the result in the output buffer @out_buf an 93 * the result in the output buffer @out_buf and the resulting length in 95 * @out_len. If the input buffer does not comp 94 * @out_len. If the input buffer does not compress, it is just copied to the 96 * @out_buf. The same happens if @compr_type i 95 * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if 97 * compression error occurred. 96 * compression error occurred. 98 * 97 * 99 * Note, if the input buffer was not compresse 98 * Note, if the input buffer was not compressed, it is copied to the output 100 * buffer and %UBIFS_COMPR_NONE is returned in 99 * buffer and %UBIFS_COMPR_NONE is returned in @compr_type. 101 */ 100 */ 102 void ubifs_compress(const struct ubifs_info *c 101 void ubifs_compress(const struct ubifs_info *c, const void *in_buf, 103 int in_len, void *out_buf, 102 int in_len, void *out_buf, int *out_len, int *compr_type) 104 { 103 { 105 int err; 104 int err; 106 struct ubifs_compressor *compr = ubifs 105 struct ubifs_compressor *compr = ubifs_compressors[*compr_type]; 107 106 108 if (*compr_type == UBIFS_COMPR_NONE) 107 if (*compr_type == UBIFS_COMPR_NONE) 109 goto no_compr; 108 goto no_compr; 110 109 111 /* If the input data is small, do not 110 /* If the input data is small, do not even try to compress it */ 112 if (in_len < UBIFS_MIN_COMPR_LEN) 111 if (in_len < UBIFS_MIN_COMPR_LEN) 113 goto no_compr; 112 goto no_compr; 114 113 115 if (compr->comp_mutex) 114 if (compr->comp_mutex) 116 mutex_lock(compr->comp_mutex); 115 mutex_lock(compr->comp_mutex); 117 err = crypto_comp_compress(compr->cc, 116 err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf, 118 (unsigned i 117 (unsigned int *)out_len); 119 if (compr->comp_mutex) 118 if (compr->comp_mutex) 120 mutex_unlock(compr->comp_mutex 119 mutex_unlock(compr->comp_mutex); 121 if (unlikely(err)) { 120 if (unlikely(err)) { 122 ubifs_warn(c, "cannot compress 121 ubifs_warn(c, "cannot compress %d bytes, compressor %s, error %d, leave data uncompressed", 123 in_len, compr->name 122 in_len, compr->name, err); 124 goto no_compr; 123 goto no_compr; 125 } 124 } 126 125 127 /* 126 /* 128 * If the data compressed only slightl 127 * If the data compressed only slightly, it is better to leave it 129 * uncompressed to improve read speed. 128 * uncompressed to improve read speed. 130 */ 129 */ 131 if (in_len - *out_len < UBIFS_MIN_COMP 130 if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF) 132 goto no_compr; 131 goto no_compr; 133 132 134 return; 133 return; 135 134 136 no_compr: 135 no_compr: 137 memcpy(out_buf, in_buf, in_len); 136 memcpy(out_buf, in_buf, in_len); 138 *out_len = in_len; 137 *out_len = in_len; 139 *compr_type = UBIFS_COMPR_NONE; 138 *compr_type = UBIFS_COMPR_NONE; 140 } 139 } 141 140 142 /** 141 /** 143 * ubifs_decompress - decompress data. 142 * ubifs_decompress - decompress data. 144 * @c: UBIFS file-system description object << 145 * @in_buf: data to decompress 143 * @in_buf: data to decompress 146 * @in_len: length of the data to decompress 144 * @in_len: length of the data to decompress 147 * @out_buf: output buffer where decompressed 145 * @out_buf: output buffer where decompressed data should 148 * @out_len: output length is returned here 146 * @out_len: output length is returned here 149 * @compr_type: type of compression 147 * @compr_type: type of compression 150 * 148 * 151 * This function decompresses data from buffer 149 * This function decompresses data from buffer @in_buf into buffer @out_buf. 152 * The length of the uncompressed data is retu 150 * The length of the uncompressed data is returned in @out_len. This functions 153 * returns %0 on success or a negative error c 151 * returns %0 on success or a negative error code on failure. 154 */ 152 */ 155 int ubifs_decompress(const struct ubifs_info * 153 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf, 156 int in_len, void *out_buf 154 int in_len, void *out_buf, int *out_len, int compr_type) 157 { 155 { 158 int err; 156 int err; 159 struct ubifs_compressor *compr; 157 struct ubifs_compressor *compr; 160 158 161 if (unlikely(compr_type < 0 || compr_t 159 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) { 162 ubifs_err(c, "invalid compress 160 ubifs_err(c, "invalid compression type %d", compr_type); 163 return -EINVAL; 161 return -EINVAL; 164 } 162 } 165 163 166 compr = ubifs_compressors[compr_type]; 164 compr = ubifs_compressors[compr_type]; 167 165 168 if (unlikely(!compr->capi_name)) { 166 if (unlikely(!compr->capi_name)) { 169 ubifs_err(c, "%s compression i 167 ubifs_err(c, "%s compression is not compiled in", compr->name); 170 return -EINVAL; 168 return -EINVAL; 171 } 169 } 172 170 173 if (compr_type == UBIFS_COMPR_NONE) { 171 if (compr_type == UBIFS_COMPR_NONE) { 174 memcpy(out_buf, in_buf, in_len 172 memcpy(out_buf, in_buf, in_len); 175 *out_len = in_len; 173 *out_len = in_len; 176 return 0; 174 return 0; 177 } 175 } 178 176 179 if (compr->decomp_mutex) 177 if (compr->decomp_mutex) 180 mutex_lock(compr->decomp_mutex 178 mutex_lock(compr->decomp_mutex); 181 err = crypto_comp_decompress(compr->cc 179 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf, 182 (unsigned 180 (unsigned int *)out_len); 183 if (compr->decomp_mutex) 181 if (compr->decomp_mutex) 184 mutex_unlock(compr->decomp_mut 182 mutex_unlock(compr->decomp_mutex); 185 if (err) 183 if (err) 186 ubifs_err(c, "cannot decompres 184 ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d", 187 in_len, compr->name, 185 in_len, compr->name, err); 188 186 189 return err; 187 return err; 190 } 188 } 191 189 192 /** 190 /** 193 * compr_init - initialize a compressor. 191 * compr_init - initialize a compressor. 194 * @compr: compressor description object 192 * @compr: compressor description object 195 * 193 * 196 * This function initializes the requested com 194 * This function initializes the requested compressor and returns zero in case 197 * of success or a negative error code in case 195 * of success or a negative error code in case of failure. 198 */ 196 */ 199 static int __init compr_init(struct ubifs_comp 197 static int __init compr_init(struct ubifs_compressor *compr) 200 { 198 { 201 if (compr->capi_name) { 199 if (compr->capi_name) { 202 compr->cc = crypto_alloc_comp( 200 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0); 203 if (IS_ERR(compr->cc)) { 201 if (IS_ERR(compr->cc)) { 204 pr_err("UBIFS error (p 202 pr_err("UBIFS error (pid %d): cannot initialize compressor %s, error %ld", 205 current->pid, c 203 current->pid, compr->name, PTR_ERR(compr->cc)); 206 return PTR_ERR(compr-> 204 return PTR_ERR(compr->cc); 207 } 205 } 208 } 206 } 209 207 210 ubifs_compressors[compr->compr_type] = 208 ubifs_compressors[compr->compr_type] = compr; 211 return 0; 209 return 0; 212 } 210 } 213 211 214 /** 212 /** 215 * compr_exit - de-initialize a compressor. 213 * compr_exit - de-initialize a compressor. 216 * @compr: compressor description object 214 * @compr: compressor description object 217 */ 215 */ 218 static void compr_exit(struct ubifs_compressor 216 static void compr_exit(struct ubifs_compressor *compr) 219 { 217 { 220 if (compr->capi_name) 218 if (compr->capi_name) 221 crypto_free_comp(compr->cc); 219 crypto_free_comp(compr->cc); >> 220 return; 222 } 221 } 223 222 224 /** 223 /** 225 * ubifs_compressors_init - initialize UBIFS c 224 * ubifs_compressors_init - initialize UBIFS compressors. 226 * 225 * 227 * This function initializes the compressor wh 226 * This function initializes the compressor which were compiled in. Returns 228 * zero in case of success and a negative erro 227 * zero in case of success and a negative error code in case of failure. 229 */ 228 */ 230 int __init ubifs_compressors_init(void) 229 int __init ubifs_compressors_init(void) 231 { 230 { 232 int err; 231 int err; 233 232 234 err = compr_init(&lzo_compr); 233 err = compr_init(&lzo_compr); 235 if (err) 234 if (err) 236 return err; 235 return err; 237 236 238 err = compr_init(&zstd_compr); 237 err = compr_init(&zstd_compr); 239 if (err) 238 if (err) 240 goto out_lzo; 239 goto out_lzo; 241 240 242 err = compr_init(&zlib_compr); 241 err = compr_init(&zlib_compr); 243 if (err) 242 if (err) 244 goto out_zstd; 243 goto out_zstd; 245 244 246 ubifs_compressors[UBIFS_COMPR_NONE] = 245 ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr; 247 return 0; 246 return 0; 248 247 249 out_zstd: 248 out_zstd: 250 compr_exit(&zstd_compr); 249 compr_exit(&zstd_compr); 251 out_lzo: 250 out_lzo: 252 compr_exit(&lzo_compr); 251 compr_exit(&lzo_compr); 253 return err; 252 return err; 254 } 253 } 255 254 256 /** 255 /** 257 * ubifs_compressors_exit - de-initialize UBIF 256 * ubifs_compressors_exit - de-initialize UBIFS compressors. 258 */ 257 */ 259 void ubifs_compressors_exit(void) 258 void ubifs_compressors_exit(void) 260 { 259 { 261 compr_exit(&lzo_compr); 260 compr_exit(&lzo_compr); 262 compr_exit(&zlib_compr); 261 compr_exit(&zlib_compr); 263 compr_exit(&zstd_compr); 262 compr_exit(&zstd_compr); 264 } 263 } 265 264
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.