compress.c 6.6 KB

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