compress.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #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. void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
  87. int in_len, void *out_buf, int *out_len, int *compr_type)
  88. {
  89. int err;
  90. struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
  91. if (*compr_type == UBIFS_COMPR_NONE)
  92. goto no_compr;
  93. /* If the input data is small, do not even try to compress it */
  94. if (in_len < UBIFS_MIN_COMPR_LEN)
  95. goto no_compr;
  96. if (compr->comp_mutex)
  97. mutex_lock(compr->comp_mutex);
  98. err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
  99. (unsigned int *)out_len);
  100. if (compr->comp_mutex)
  101. mutex_unlock(compr->comp_mutex);
  102. if (unlikely(err)) {
  103. ubifs_warn(c, "cannot compress %d bytes, compressor %s, error %d, leave data uncompressed",
  104. in_len, compr->name, err);
  105. goto no_compr;
  106. }
  107. /*
  108. * If the data compressed only slightly, it is better to leave it
  109. * uncompressed to improve read speed.
  110. */
  111. if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
  112. goto no_compr;
  113. return;
  114. no_compr:
  115. memcpy(out_buf, in_buf, in_len);
  116. *out_len = in_len;
  117. *compr_type = UBIFS_COMPR_NONE;
  118. }
  119. /**
  120. * ubifs_decompress - decompress data.
  121. * @in_buf: data to decompress
  122. * @in_len: length of the data to decompress
  123. * @out_buf: output buffer where decompressed data should
  124. * @out_len: output length is returned here
  125. * @compr_type: type of compression
  126. *
  127. * This function decompresses data from buffer @in_buf into buffer @out_buf.
  128. * The length of the uncompressed data is returned in @out_len. This functions
  129. * returns %0 on success or a negative error code on failure.
  130. */
  131. int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
  132. int in_len, void *out_buf, int *out_len, int compr_type)
  133. {
  134. int err;
  135. struct ubifs_compressor *compr;
  136. if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
  137. ubifs_err(c, "invalid compression type %d", compr_type);
  138. return -EINVAL;
  139. }
  140. compr = ubifs_compressors[compr_type];
  141. if (unlikely(!compr->capi_name)) {
  142. ubifs_err(c, "%s compression is not compiled in", compr->name);
  143. return -EINVAL;
  144. }
  145. if (compr_type == UBIFS_COMPR_NONE) {
  146. memcpy(out_buf, in_buf, in_len);
  147. *out_len = in_len;
  148. return 0;
  149. }
  150. if (compr->decomp_mutex)
  151. mutex_lock(compr->decomp_mutex);
  152. err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
  153. (unsigned int *)out_len);
  154. if (compr->decomp_mutex)
  155. mutex_unlock(compr->decomp_mutex);
  156. if (err)
  157. ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
  158. in_len, compr->name, err);
  159. return err;
  160. }
  161. /**
  162. * compr_init - initialize a compressor.
  163. * @compr: compressor description object
  164. *
  165. * This function initializes the requested compressor and returns zero in case
  166. * of success or a negative error code in case of failure.
  167. */
  168. static int __init compr_init(struct ubifs_compressor *compr)
  169. {
  170. if (compr->capi_name) {
  171. compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
  172. if (IS_ERR(compr->cc)) {
  173. pr_err("UBIFS error (pid %d): cannot initialize compressor %s, error %ld",
  174. current->pid, compr->name, PTR_ERR(compr->cc));
  175. return PTR_ERR(compr->cc);
  176. }
  177. }
  178. ubifs_compressors[compr->compr_type] = compr;
  179. return 0;
  180. }
  181. /**
  182. * compr_exit - de-initialize a compressor.
  183. * @compr: compressor description object
  184. */
  185. static void compr_exit(struct ubifs_compressor *compr)
  186. {
  187. if (compr->capi_name)
  188. crypto_free_comp(compr->cc);
  189. return;
  190. }
  191. /**
  192. * ubifs_compressors_init - initialize UBIFS compressors.
  193. *
  194. * This function initializes the compressor which were compiled in. Returns
  195. * zero in case of success and a negative error code in case of failure.
  196. */
  197. int __init ubifs_compressors_init(void)
  198. {
  199. int err;
  200. err = compr_init(&lzo_compr);
  201. if (err)
  202. return err;
  203. err = compr_init(&zlib_compr);
  204. if (err)
  205. goto out_lzo;
  206. ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
  207. return 0;
  208. out_lzo:
  209. compr_exit(&lzo_compr);
  210. return err;
  211. }
  212. /**
  213. * ubifs_compressors_exit - de-initialize UBIFS compressors.
  214. */
  215. void ubifs_compressors_exit(void)
  216. {
  217. compr_exit(&lzo_compr);
  218. compr_exit(&zlib_compr);
  219. }