compress.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 = "no compression",
  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. #ifdef CONFIG_UBIFS_FS_ZLIB
  51. static DEFINE_MUTEX(deflate_mutex);
  52. static DEFINE_MUTEX(inflate_mutex);
  53. static struct ubifs_compressor zlib_compr = {
  54. .compr_type = UBIFS_COMPR_ZLIB,
  55. .comp_mutex = &deflate_mutex,
  56. .decomp_mutex = &inflate_mutex,
  57. .name = "zlib",
  58. .capi_name = "deflate",
  59. };
  60. #else
  61. static struct ubifs_compressor zlib_compr = {
  62. .compr_type = UBIFS_COMPR_ZLIB,
  63. .name = "zlib",
  64. };
  65. #endif
  66. /* All UBIFS compressors */
  67. struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
  68. /**
  69. * ubifs_compress - compress data.
  70. * @in_buf: data to compress
  71. * @in_len: length of the data to compress
  72. * @out_buf: output buffer where compressed data should be stored
  73. * @out_len: output buffer length is returned here
  74. * @compr_type: type of compression to use on enter, actually used compression
  75. * type on exit
  76. *
  77. * This function compresses input buffer @in_buf of length @in_len and stores
  78. * the result in the output buffer @out_buf and the resulting length in
  79. * @out_len. If the input buffer does not compress, it is just copied to the
  80. * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if
  81. * compression error occurred.
  82. *
  83. * Note, if the input buffer was not compressed, it is copied to the output
  84. * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
  85. *
  86. * This functions returns %0 on success or a negative error code on failure.
  87. */
  88. void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
  89. int *compr_type)
  90. {
  91. int err;
  92. struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
  93. if (*compr_type == UBIFS_COMPR_NONE)
  94. goto no_compr;
  95. /* If the input data is small, do not even try to compress it */
  96. if (in_len < UBIFS_MIN_COMPR_LEN)
  97. goto no_compr;
  98. if (compr->comp_mutex)
  99. mutex_lock(compr->comp_mutex);
  100. err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
  101. out_len);
  102. if (compr->comp_mutex)
  103. mutex_unlock(compr->comp_mutex);
  104. if (unlikely(err)) {
  105. ubifs_warn("cannot compress %d bytes, compressor %s, "
  106. "error %d, leave data uncompressed",
  107. in_len, compr->name, err);
  108. goto no_compr;
  109. }
  110. /*
  111. * Presently, we just require that compression results in less data,
  112. * rather than any defined minimum compression ratio or amount.
  113. */
  114. if (ALIGN(*out_len, 8) >= ALIGN(in_len, 8))
  115. goto no_compr;
  116. return;
  117. no_compr:
  118. memcpy(out_buf, in_buf, in_len);
  119. *out_len = in_len;
  120. *compr_type = UBIFS_COMPR_NONE;
  121. }
  122. /**
  123. * ubifs_decompress - decompress data.
  124. * @in_buf: data to decompress
  125. * @in_len: length of the data to decompress
  126. * @out_buf: output buffer where decompressed data should
  127. * @out_len: output length is returned here
  128. * @compr_type: type of compression
  129. *
  130. * This function decompresses data from buffer @in_buf into buffer @out_buf.
  131. * The length of the uncompressed data is returned in @out_len. This functions
  132. * returns %0 on success or a negative error code on failure.
  133. */
  134. int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
  135. int *out_len, int compr_type)
  136. {
  137. int err;
  138. struct ubifs_compressor *compr;
  139. if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
  140. ubifs_err("invalid compression type %d", compr_type);
  141. return -EINVAL;
  142. }
  143. compr = ubifs_compressors[compr_type];
  144. if (unlikely(!compr->capi_name)) {
  145. ubifs_err("%s compression is not compiled in", compr->name);
  146. return -EINVAL;
  147. }
  148. if (compr_type == UBIFS_COMPR_NONE) {
  149. memcpy(out_buf, in_buf, in_len);
  150. *out_len = in_len;
  151. return 0;
  152. }
  153. if (compr->decomp_mutex)
  154. mutex_lock(compr->decomp_mutex);
  155. err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
  156. out_len);
  157. if (compr->decomp_mutex)
  158. mutex_unlock(compr->decomp_mutex);
  159. if (err)
  160. ubifs_err("cannot decompress %d bytes, compressor %s, "
  161. "error %d", in_len, compr->name, err);
  162. return err;
  163. }
  164. /**
  165. * compr_init - initialize a compressor.
  166. * @compr: compressor description object
  167. *
  168. * This function initializes the requested compressor and returns zero in case
  169. * of success or a negative error code in case of failure.
  170. */
  171. static int __init compr_init(struct ubifs_compressor *compr)
  172. {
  173. if (compr->capi_name) {
  174. compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
  175. if (IS_ERR(compr->cc)) {
  176. ubifs_err("cannot initialize compressor %s, error %ld",
  177. compr->name, PTR_ERR(compr->cc));
  178. return PTR_ERR(compr->cc);
  179. }
  180. }
  181. ubifs_compressors[compr->compr_type] = compr;
  182. return 0;
  183. }
  184. /**
  185. * compr_exit - de-initialize a compressor.
  186. * @compr: compressor description object
  187. */
  188. static void compr_exit(struct ubifs_compressor *compr)
  189. {
  190. if (compr->capi_name)
  191. crypto_free_comp(compr->cc);
  192. return;
  193. }
  194. /**
  195. * ubifs_compressors_init - initialize UBIFS compressors.
  196. *
  197. * This function initializes the compressor which were compiled in. Returns
  198. * zero in case of success and a negative error code in case of failure.
  199. */
  200. int __init ubifs_compressors_init(void)
  201. {
  202. int err;
  203. err = compr_init(&lzo_compr);
  204. if (err)
  205. return err;
  206. err = compr_init(&zlib_compr);
  207. if (err)
  208. goto out_lzo;
  209. ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
  210. return 0;
  211. out_lzo:
  212. compr_exit(&lzo_compr);
  213. return err;
  214. }
  215. /**
  216. * ubifs_compressors_exit - de-initialize UBIFS compressors.
  217. */
  218. void __exit ubifs_compressors_exit(void)
  219. {
  220. compr_exit(&lzo_compr);
  221. compr_exit(&zlib_compr);
  222. }