compr.c 10 KB

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