compress.c 7.0 KB

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