compr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 mode, 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. if (c->mount_opts.override_compr)
  76. mode = c->mount_opts.compr;
  77. else
  78. mode = jffs2_compression_mode;
  79. switch (mode) {
  80. case JFFS2_COMPR_MODE_NONE:
  81. break;
  82. case JFFS2_COMPR_MODE_PRIORITY:
  83. output_buf = kmalloc(*cdatalen,GFP_KERNEL);
  84. if (!output_buf) {
  85. printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
  86. goto out;
  87. }
  88. orig_slen = *datalen;
  89. orig_dlen = *cdatalen;
  90. spin_lock(&jffs2_compressor_list_lock);
  91. list_for_each_entry(this, &jffs2_compressor_list, list) {
  92. /* Skip decompress-only backwards-compatibility and disabled modules */
  93. if ((!this->compress)||(this->disabled))
  94. continue;
  95. this->usecount++;
  96. spin_unlock(&jffs2_compressor_list_lock);
  97. *datalen = orig_slen;
  98. *cdatalen = orig_dlen;
  99. compr_ret = this->compress(data_in, output_buf, datalen, cdatalen);
  100. spin_lock(&jffs2_compressor_list_lock);
  101. this->usecount--;
  102. if (!compr_ret) {
  103. ret = this->compr;
  104. this->stat_compr_blocks++;
  105. this->stat_compr_orig_size += *datalen;
  106. this->stat_compr_new_size += *cdatalen;
  107. break;
  108. }
  109. }
  110. spin_unlock(&jffs2_compressor_list_lock);
  111. if (ret == JFFS2_COMPR_NONE)
  112. kfree(output_buf);
  113. break;
  114. case JFFS2_COMPR_MODE_SIZE:
  115. case JFFS2_COMPR_MODE_FAVOURLZO:
  116. orig_slen = *datalen;
  117. orig_dlen = *cdatalen;
  118. spin_lock(&jffs2_compressor_list_lock);
  119. list_for_each_entry(this, &jffs2_compressor_list, list) {
  120. /* Skip decompress-only backwards-compatibility and disabled modules */
  121. if ((!this->compress)||(this->disabled))
  122. continue;
  123. /* Allocating memory for output buffer if necessary */
  124. if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) {
  125. spin_unlock(&jffs2_compressor_list_lock);
  126. kfree(this->compr_buf);
  127. spin_lock(&jffs2_compressor_list_lock);
  128. this->compr_buf_size=0;
  129. this->compr_buf=NULL;
  130. }
  131. if (!this->compr_buf) {
  132. spin_unlock(&jffs2_compressor_list_lock);
  133. tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
  134. spin_lock(&jffs2_compressor_list_lock);
  135. if (!tmp_buf) {
  136. printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n", orig_slen);
  137. continue;
  138. }
  139. else {
  140. this->compr_buf = tmp_buf;
  141. this->compr_buf_size = orig_slen;
  142. }
  143. }
  144. this->usecount++;
  145. spin_unlock(&jffs2_compressor_list_lock);
  146. *datalen = orig_slen;
  147. *cdatalen = orig_dlen;
  148. compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
  149. spin_lock(&jffs2_compressor_list_lock);
  150. this->usecount--;
  151. if (!compr_ret) {
  152. if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
  153. && (*cdatalen < *datalen)) {
  154. best_dlen = *cdatalen;
  155. best_slen = *datalen;
  156. best = this;
  157. }
  158. }
  159. }
  160. if (best_dlen) {
  161. *cdatalen = best_dlen;
  162. *datalen = best_slen;
  163. output_buf = best->compr_buf;
  164. best->compr_buf = NULL;
  165. best->compr_buf_size = 0;
  166. best->stat_compr_blocks++;
  167. best->stat_compr_orig_size += best_slen;
  168. best->stat_compr_new_size += best_dlen;
  169. ret = best->compr;
  170. }
  171. spin_unlock(&jffs2_compressor_list_lock);
  172. break;
  173. default:
  174. printk(KERN_ERR "JFFS2: unknown compression mode.\n");
  175. }
  176. out:
  177. if (ret == JFFS2_COMPR_NONE) {
  178. *cpage_out = data_in;
  179. *datalen = *cdatalen;
  180. none_stat_compr_blocks++;
  181. none_stat_compr_size += *datalen;
  182. }
  183. else {
  184. *cpage_out = output_buf;
  185. }
  186. return ret;
  187. }
  188. int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  189. uint16_t comprtype, unsigned char *cdata_in,
  190. unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
  191. {
  192. struct jffs2_compressor *this;
  193. int ret;
  194. /* Older code had a bug where it would write non-zero 'usercompr'
  195. fields. Deal with it. */
  196. if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
  197. comprtype &= 0xff;
  198. switch (comprtype & 0xff) {
  199. case JFFS2_COMPR_NONE:
  200. /* This should be special-cased elsewhere, but we might as well deal with it */
  201. memcpy(data_out, cdata_in, datalen);
  202. none_stat_decompr_blocks++;
  203. break;
  204. case JFFS2_COMPR_ZERO:
  205. memset(data_out, 0, datalen);
  206. break;
  207. default:
  208. spin_lock(&jffs2_compressor_list_lock);
  209. list_for_each_entry(this, &jffs2_compressor_list, list) {
  210. if (comprtype == this->compr) {
  211. this->usecount++;
  212. spin_unlock(&jffs2_compressor_list_lock);
  213. ret = this->decompress(cdata_in, data_out, cdatalen, datalen);
  214. spin_lock(&jffs2_compressor_list_lock);
  215. if (ret) {
  216. printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
  217. }
  218. else {
  219. this->stat_decompr_blocks++;
  220. }
  221. this->usecount--;
  222. spin_unlock(&jffs2_compressor_list_lock);
  223. return ret;
  224. }
  225. }
  226. printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
  227. spin_unlock(&jffs2_compressor_list_lock);
  228. return -EIO;
  229. }
  230. return 0;
  231. }
  232. int jffs2_register_compressor(struct jffs2_compressor *comp)
  233. {
  234. struct jffs2_compressor *this;
  235. if (!comp->name) {
  236. printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
  237. return -1;
  238. }
  239. comp->compr_buf_size=0;
  240. comp->compr_buf=NULL;
  241. comp->usecount=0;
  242. comp->stat_compr_orig_size=0;
  243. comp->stat_compr_new_size=0;
  244. comp->stat_compr_blocks=0;
  245. comp->stat_decompr_blocks=0;
  246. D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
  247. spin_lock(&jffs2_compressor_list_lock);
  248. list_for_each_entry(this, &jffs2_compressor_list, list) {
  249. if (this->priority < comp->priority) {
  250. list_add(&comp->list, this->list.prev);
  251. goto out;
  252. }
  253. }
  254. list_add_tail(&comp->list, &jffs2_compressor_list);
  255. out:
  256. D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
  257. printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
  258. })
  259. spin_unlock(&jffs2_compressor_list_lock);
  260. return 0;
  261. }
  262. int jffs2_unregister_compressor(struct jffs2_compressor *comp)
  263. {
  264. D2(struct jffs2_compressor *this;)
  265. D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
  266. spin_lock(&jffs2_compressor_list_lock);
  267. if (comp->usecount) {
  268. spin_unlock(&jffs2_compressor_list_lock);
  269. printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
  270. return -1;
  271. }
  272. list_del(&comp->list);
  273. D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
  274. printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
  275. })
  276. spin_unlock(&jffs2_compressor_list_lock);
  277. return 0;
  278. }
  279. void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
  280. {
  281. if (orig != comprbuf)
  282. kfree(comprbuf);
  283. }
  284. int __init jffs2_compressors_init(void)
  285. {
  286. /* Registering compressors */
  287. #ifdef CONFIG_JFFS2_ZLIB
  288. jffs2_zlib_init();
  289. #endif
  290. #ifdef CONFIG_JFFS2_RTIME
  291. jffs2_rtime_init();
  292. #endif
  293. #ifdef CONFIG_JFFS2_RUBIN
  294. jffs2_rubinmips_init();
  295. jffs2_dynrubin_init();
  296. #endif
  297. #ifdef CONFIG_JFFS2_LZO
  298. jffs2_lzo_init();
  299. #endif
  300. /* Setting default compression mode */
  301. #ifdef CONFIG_JFFS2_CMODE_NONE
  302. jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
  303. D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
  304. #else
  305. #ifdef CONFIG_JFFS2_CMODE_SIZE
  306. jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
  307. D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
  308. #else
  309. #ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
  310. jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
  311. D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
  312. #else
  313. D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
  314. #endif
  315. #endif
  316. #endif
  317. return 0;
  318. }
  319. int jffs2_compressors_exit(void)
  320. {
  321. /* Unregistering compressors */
  322. #ifdef CONFIG_JFFS2_LZO
  323. jffs2_lzo_exit();
  324. #endif
  325. #ifdef CONFIG_JFFS2_RUBIN
  326. jffs2_dynrubin_exit();
  327. jffs2_rubinmips_exit();
  328. #endif
  329. #ifdef CONFIG_JFFS2_RTIME
  330. jffs2_rtime_exit();
  331. #endif
  332. #ifdef CONFIG_JFFS2_ZLIB
  333. jffs2_zlib_exit();
  334. #endif
  335. return 0;
  336. }