compr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
  6. * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
  7. * University of Szeged, Hungary
  8. *
  9. * Created by Arjan van de Ven <arjan@infradead.org>
  10. *
  11. * For licensing information, see the file 'LICENCE' in this directory.
  12. *
  13. */
  14. #include "compr.h"
  15. static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
  16. /* Available compressors are on this list */
  17. static LIST_HEAD(jffs2_compressor_list);
  18. /* Actual compression mode */
  19. static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
  20. /* Statistics for blocks stored without compression */
  21. static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
  22. /*
  23. * Return 1 to use this compression
  24. */
  25. static int jffs2_is_best_compression(struct jffs2_compressor *this,
  26. struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
  27. {
  28. switch (jffs2_compression_mode) {
  29. case JFFS2_COMPR_MODE_SIZE:
  30. if (bestsize > size)
  31. return 1;
  32. return 0;
  33. case JFFS2_COMPR_MODE_FAVOURLZO:
  34. if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
  35. return 1;
  36. if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
  37. return 1;
  38. if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
  39. return 1;
  40. if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
  41. return 1;
  42. return 0;
  43. }
  44. /* Shouldn't happen */
  45. return 0;
  46. }
  47. /* jffs2_compress:
  48. * @data_in: Pointer to uncompressed data
  49. * @cpage_out: Pointer to returned pointer to buffer for compressed data
  50. * @datalen: On entry, holds the amount of data available for compression.
  51. * On exit, expected to hold the amount of data actually compressed.
  52. * @cdatalen: On entry, holds the amount of space available for compressed
  53. * data. On exit, expected to hold the actual size of the compressed
  54. * data.
  55. *
  56. * Returns: Lower byte to be stored with data indicating compression type used.
  57. * Zero is used to show that the data could not be compressed - the
  58. * compressed version was actually larger than the original.
  59. * Upper byte will be used later. (soon)
  60. *
  61. * If the cdata buffer isn't large enough to hold all the uncompressed data,
  62. * jffs2_compress should compress as much as will fit, and should set
  63. * *datalen accordingly to show the amount of data which were compressed.
  64. */
  65. uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  66. unsigned char *data_in, unsigned char **cpage_out,
  67. uint32_t *datalen, uint32_t *cdatalen)
  68. {
  69. int ret = JFFS2_COMPR_NONE;
  70. int compr_ret;
  71. struct jffs2_compressor *this, *best=NULL;
  72. unsigned char *output_buf = NULL, *tmp_buf;
  73. uint32_t orig_slen, orig_dlen;
  74. uint32_t best_slen=0, best_dlen=0;
  75. switch (jffs2_compression_mode) {
  76. case JFFS2_COMPR_MODE_NONE:
  77. break;
  78. case JFFS2_COMPR_MODE_PRIORITY:
  79. output_buf = kmalloc(*cdatalen,GFP_KERNEL);
  80. if (!output_buf) {
  81. printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
  82. goto out;
  83. }
  84. orig_slen = *datalen;
  85. orig_dlen = *cdatalen;
  86. spin_lock(&jffs2_compressor_list_lock);
  87. list_for_each_entry(this, &jffs2_compressor_list, list) {
  88. /* Skip decompress-only backwards-compatibility and disabled modules */
  89. if ((!this->compress)||(this->disabled))
  90. continue;
  91. this->usecount++;
  92. spin_unlock(&jffs2_compressor_list_lock);
  93. *datalen = orig_slen;
  94. *cdatalen = orig_dlen;
  95. compr_ret = this->compress(data_in, output_buf, datalen, cdatalen);
  96. spin_lock(&jffs2_compressor_list_lock);
  97. this->usecount--;
  98. if (!compr_ret) {
  99. ret = this->compr;
  100. this->stat_compr_blocks++;
  101. this->stat_compr_orig_size += *datalen;
  102. this->stat_compr_new_size += *cdatalen;
  103. break;
  104. }
  105. }
  106. spin_unlock(&jffs2_compressor_list_lock);
  107. if (ret == JFFS2_COMPR_NONE)
  108. kfree(output_buf);
  109. break;
  110. case JFFS2_COMPR_MODE_SIZE:
  111. case JFFS2_COMPR_MODE_FAVOURLZO:
  112. orig_slen = *datalen;
  113. orig_dlen = *cdatalen;
  114. spin_lock(&jffs2_compressor_list_lock);
  115. list_for_each_entry(this, &jffs2_compressor_list, list) {
  116. /* Skip decompress-only backwards-compatibility and disabled modules */
  117. if ((!this->compress)||(this->disabled))
  118. continue;
  119. /* Allocating memory for output buffer if necessary */
  120. if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) {
  121. spin_unlock(&jffs2_compressor_list_lock);
  122. kfree(this->compr_buf);
  123. spin_lock(&jffs2_compressor_list_lock);
  124. this->compr_buf_size=0;
  125. this->compr_buf=NULL;
  126. }
  127. if (!this->compr_buf) {
  128. spin_unlock(&jffs2_compressor_list_lock);
  129. tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
  130. spin_lock(&jffs2_compressor_list_lock);
  131. if (!tmp_buf) {
  132. printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n", orig_slen);
  133. continue;
  134. }
  135. else {
  136. this->compr_buf = tmp_buf;
  137. this->compr_buf_size = orig_slen;
  138. }
  139. }
  140. this->usecount++;
  141. spin_unlock(&jffs2_compressor_list_lock);
  142. *datalen = orig_slen;
  143. *cdatalen = orig_dlen;
  144. compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
  145. spin_lock(&jffs2_compressor_list_lock);
  146. this->usecount--;
  147. if (!compr_ret) {
  148. if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
  149. && (*cdatalen < *datalen)) {
  150. best_dlen = *cdatalen;
  151. best_slen = *datalen;
  152. best = this;
  153. }
  154. }
  155. }
  156. if (best_dlen) {
  157. *cdatalen = best_dlen;
  158. *datalen = best_slen;
  159. output_buf = best->compr_buf;
  160. best->compr_buf = NULL;
  161. best->compr_buf_size = 0;
  162. best->stat_compr_blocks++;
  163. best->stat_compr_orig_size += best_slen;
  164. best->stat_compr_new_size += best_dlen;
  165. ret = best->compr;
  166. }
  167. spin_unlock(&jffs2_compressor_list_lock);
  168. break;
  169. default:
  170. printk(KERN_ERR "JFFS2: unknown compression mode.\n");
  171. }
  172. out:
  173. if (ret == JFFS2_COMPR_NONE) {
  174. *cpage_out = data_in;
  175. *datalen = *cdatalen;
  176. none_stat_compr_blocks++;
  177. none_stat_compr_size += *datalen;
  178. }
  179. else {
  180. *cpage_out = output_buf;
  181. }
  182. return ret;
  183. }
  184. int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  185. uint16_t comprtype, unsigned char *cdata_in,
  186. unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
  187. {
  188. struct jffs2_compressor *this;
  189. int ret;
  190. /* Older code had a bug where it would write non-zero 'usercompr'
  191. fields. Deal with it. */
  192. if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
  193. comprtype &= 0xff;
  194. switch (comprtype & 0xff) {
  195. case JFFS2_COMPR_NONE:
  196. /* This should be special-cased elsewhere, but we might as well deal with it */
  197. memcpy(data_out, cdata_in, datalen);
  198. none_stat_decompr_blocks++;
  199. break;
  200. case JFFS2_COMPR_ZERO:
  201. memset(data_out, 0, datalen);
  202. break;
  203. default:
  204. spin_lock(&jffs2_compressor_list_lock);
  205. list_for_each_entry(this, &jffs2_compressor_list, list) {
  206. if (comprtype == this->compr) {
  207. this->usecount++;
  208. spin_unlock(&jffs2_compressor_list_lock);
  209. ret = this->decompress(cdata_in, data_out, cdatalen, datalen);
  210. spin_lock(&jffs2_compressor_list_lock);
  211. if (ret) {
  212. printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
  213. }
  214. else {
  215. this->stat_decompr_blocks++;
  216. }
  217. this->usecount--;
  218. spin_unlock(&jffs2_compressor_list_lock);
  219. return ret;
  220. }
  221. }
  222. printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
  223. spin_unlock(&jffs2_compressor_list_lock);
  224. return -EIO;
  225. }
  226. return 0;
  227. }
  228. int jffs2_register_compressor(struct jffs2_compressor *comp)
  229. {
  230. struct jffs2_compressor *this;
  231. if (!comp->name) {
  232. printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
  233. return -1;
  234. }
  235. comp->compr_buf_size=0;
  236. comp->compr_buf=NULL;
  237. comp->usecount=0;
  238. comp->stat_compr_orig_size=0;
  239. comp->stat_compr_new_size=0;
  240. comp->stat_compr_blocks=0;
  241. comp->stat_decompr_blocks=0;
  242. D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
  243. spin_lock(&jffs2_compressor_list_lock);
  244. list_for_each_entry(this, &jffs2_compressor_list, list) {
  245. if (this->priority < comp->priority) {
  246. list_add(&comp->list, this->list.prev);
  247. goto out;
  248. }
  249. }
  250. list_add_tail(&comp->list, &jffs2_compressor_list);
  251. out:
  252. D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
  253. printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
  254. })
  255. spin_unlock(&jffs2_compressor_list_lock);
  256. return 0;
  257. }
  258. int jffs2_unregister_compressor(struct jffs2_compressor *comp)
  259. {
  260. D2(struct jffs2_compressor *this;)
  261. D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
  262. spin_lock(&jffs2_compressor_list_lock);
  263. if (comp->usecount) {
  264. spin_unlock(&jffs2_compressor_list_lock);
  265. printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
  266. return -1;
  267. }
  268. list_del(&comp->list);
  269. D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
  270. printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
  271. })
  272. spin_unlock(&jffs2_compressor_list_lock);
  273. return 0;
  274. }
  275. void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
  276. {
  277. if (orig != comprbuf)
  278. kfree(comprbuf);
  279. }
  280. int __init jffs2_compressors_init(void)
  281. {
  282. /* Registering compressors */
  283. #ifdef CONFIG_JFFS2_ZLIB
  284. jffs2_zlib_init();
  285. #endif
  286. #ifdef CONFIG_JFFS2_RTIME
  287. jffs2_rtime_init();
  288. #endif
  289. #ifdef CONFIG_JFFS2_RUBIN
  290. jffs2_rubinmips_init();
  291. jffs2_dynrubin_init();
  292. #endif
  293. #ifdef CONFIG_JFFS2_LZO
  294. jffs2_lzo_init();
  295. #endif
  296. /* Setting default compression mode */
  297. #ifdef CONFIG_JFFS2_CMODE_NONE
  298. jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
  299. D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
  300. #else
  301. #ifdef CONFIG_JFFS2_CMODE_SIZE
  302. jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
  303. D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
  304. #else
  305. #ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
  306. jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
  307. D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
  308. #else
  309. D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
  310. #endif
  311. #endif
  312. #endif
  313. return 0;
  314. }
  315. int jffs2_compressors_exit(void)
  316. {
  317. /* Unregistering compressors */
  318. #ifdef CONFIG_JFFS2_LZO
  319. jffs2_lzo_exit();
  320. #endif
  321. #ifdef CONFIG_JFFS2_RUBIN
  322. jffs2_dynrubin_exit();
  323. jffs2_rubinmips_exit();
  324. #endif
  325. #ifdef CONFIG_JFFS2_RTIME
  326. jffs2_rtime_exit();
  327. #endif
  328. #ifdef CONFIG_JFFS2_ZLIB
  329. jffs2_zlib_exit();
  330. #endif
  331. return 0;
  332. }