compr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. /*
  48. * jffs2_selected_compress:
  49. * @compr: Explicit compression type to use (ie, JFFS2_COMPR_ZLIB).
  50. * If 0, just take the first available compression mode.
  51. * @data_in: Pointer to uncompressed data
  52. * @cpage_out: Pointer to returned pointer to buffer for compressed data
  53. * @datalen: On entry, holds the amount of data available for compression.
  54. * On exit, expected to hold the amount of data actually compressed.
  55. * @cdatalen: On entry, holds the amount of space available for compressed
  56. * data. On exit, expected to hold the actual size of the compressed
  57. * data.
  58. *
  59. * Returns: the compression type used. Zero is used to show that the data
  60. * could not be compressed; probably because we couldn't find the requested
  61. * compression mode.
  62. */
  63. static int jffs2_selected_compress(u8 compr, unsigned char *data_in,
  64. unsigned char **cpage_out, u32 *datalen, u32 *cdatalen)
  65. {
  66. struct jffs2_compressor *this;
  67. int err, ret = JFFS2_COMPR_NONE;
  68. uint32_t orig_slen, orig_dlen;
  69. char *output_buf;
  70. output_buf = kmalloc(*cdatalen, GFP_KERNEL);
  71. if (!output_buf) {
  72. printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
  73. return ret;
  74. }
  75. orig_slen = *datalen;
  76. orig_dlen = *cdatalen;
  77. spin_lock(&jffs2_compressor_list_lock);
  78. list_for_each_entry(this, &jffs2_compressor_list, list) {
  79. /* Skip decompress-only and disabled modules */
  80. if (!this->compress || this->disabled)
  81. continue;
  82. /* Skip if not the desired compression type */
  83. if (compr && (compr != this->compr))
  84. continue;
  85. /*
  86. * Either compression type was unspecified, or we found our
  87. * compressor; either way, we're good to go.
  88. */
  89. this->usecount++;
  90. spin_unlock(&jffs2_compressor_list_lock);
  91. *datalen = orig_slen;
  92. *cdatalen = orig_dlen;
  93. err = this->compress(data_in, output_buf, datalen, cdatalen);
  94. spin_lock(&jffs2_compressor_list_lock);
  95. this->usecount--;
  96. if (!err) {
  97. /* Success */
  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. else
  109. *cpage_out = output_buf;
  110. return ret;
  111. }
  112. /* jffs2_compress:
  113. * @data_in: Pointer to uncompressed data
  114. * @cpage_out: Pointer to returned pointer to buffer for compressed data
  115. * @datalen: On entry, holds the amount of data available for compression.
  116. * On exit, expected to hold the amount of data actually compressed.
  117. * @cdatalen: On entry, holds the amount of space available for compressed
  118. * data. On exit, expected to hold the actual size of the compressed
  119. * data.
  120. *
  121. * Returns: Lower byte to be stored with data indicating compression type used.
  122. * Zero is used to show that the data could not be compressed - the
  123. * compressed version was actually larger than the original.
  124. * Upper byte will be used later. (soon)
  125. *
  126. * If the cdata buffer isn't large enough to hold all the uncompressed data,
  127. * jffs2_compress should compress as much as will fit, and should set
  128. * *datalen accordingly to show the amount of data which were compressed.
  129. */
  130. uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  131. unsigned char *data_in, unsigned char **cpage_out,
  132. uint32_t *datalen, uint32_t *cdatalen)
  133. {
  134. int ret = JFFS2_COMPR_NONE;
  135. int mode, compr_ret;
  136. struct jffs2_compressor *this, *best=NULL;
  137. unsigned char *output_buf = NULL, *tmp_buf;
  138. uint32_t orig_slen, orig_dlen;
  139. uint32_t best_slen=0, best_dlen=0;
  140. if (c->mount_opts.override_compr)
  141. mode = c->mount_opts.compr;
  142. else
  143. mode = jffs2_compression_mode;
  144. switch (mode) {
  145. case JFFS2_COMPR_MODE_NONE:
  146. break;
  147. case JFFS2_COMPR_MODE_PRIORITY:
  148. ret = jffs2_selected_compress(0, data_in, cpage_out, datalen,
  149. cdatalen);
  150. break;
  151. case JFFS2_COMPR_MODE_SIZE:
  152. case JFFS2_COMPR_MODE_FAVOURLZO:
  153. orig_slen = *datalen;
  154. orig_dlen = *cdatalen;
  155. spin_lock(&jffs2_compressor_list_lock);
  156. list_for_each_entry(this, &jffs2_compressor_list, list) {
  157. /* Skip decompress-only backwards-compatibility and disabled modules */
  158. if ((!this->compress)||(this->disabled))
  159. continue;
  160. /* Allocating memory for output buffer if necessary */
  161. if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) {
  162. spin_unlock(&jffs2_compressor_list_lock);
  163. kfree(this->compr_buf);
  164. spin_lock(&jffs2_compressor_list_lock);
  165. this->compr_buf_size=0;
  166. this->compr_buf=NULL;
  167. }
  168. if (!this->compr_buf) {
  169. spin_unlock(&jffs2_compressor_list_lock);
  170. tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
  171. spin_lock(&jffs2_compressor_list_lock);
  172. if (!tmp_buf) {
  173. printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n", orig_slen);
  174. continue;
  175. }
  176. else {
  177. this->compr_buf = tmp_buf;
  178. this->compr_buf_size = orig_slen;
  179. }
  180. }
  181. this->usecount++;
  182. spin_unlock(&jffs2_compressor_list_lock);
  183. *datalen = orig_slen;
  184. *cdatalen = orig_dlen;
  185. compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
  186. spin_lock(&jffs2_compressor_list_lock);
  187. this->usecount--;
  188. if (!compr_ret) {
  189. if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
  190. && (*cdatalen < *datalen)) {
  191. best_dlen = *cdatalen;
  192. best_slen = *datalen;
  193. best = this;
  194. }
  195. }
  196. }
  197. if (best_dlen) {
  198. *cdatalen = best_dlen;
  199. *datalen = best_slen;
  200. output_buf = best->compr_buf;
  201. best->compr_buf = NULL;
  202. best->compr_buf_size = 0;
  203. best->stat_compr_blocks++;
  204. best->stat_compr_orig_size += best_slen;
  205. best->stat_compr_new_size += best_dlen;
  206. ret = best->compr;
  207. *cpage_out = output_buf;
  208. }
  209. spin_unlock(&jffs2_compressor_list_lock);
  210. break;
  211. case JFFS2_COMPR_MODE_FORCELZO:
  212. ret = jffs2_selected_compress(JFFS2_COMPR_LZO, data_in,
  213. cpage_out, datalen, cdatalen);
  214. break;
  215. case JFFS2_COMPR_MODE_FORCEZLIB:
  216. ret = jffs2_selected_compress(JFFS2_COMPR_ZLIB, data_in,
  217. cpage_out, datalen, cdatalen);
  218. break;
  219. default:
  220. printk(KERN_ERR "JFFS2: unknown compression mode.\n");
  221. }
  222. if (ret == JFFS2_COMPR_NONE) {
  223. *cpage_out = data_in;
  224. *datalen = *cdatalen;
  225. none_stat_compr_blocks++;
  226. none_stat_compr_size += *datalen;
  227. }
  228. return ret;
  229. }
  230. int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  231. uint16_t comprtype, unsigned char *cdata_in,
  232. unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
  233. {
  234. struct jffs2_compressor *this;
  235. int ret;
  236. /* Older code had a bug where it would write non-zero 'usercompr'
  237. fields. Deal with it. */
  238. if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
  239. comprtype &= 0xff;
  240. switch (comprtype & 0xff) {
  241. case JFFS2_COMPR_NONE:
  242. /* This should be special-cased elsewhere, but we might as well deal with it */
  243. memcpy(data_out, cdata_in, datalen);
  244. none_stat_decompr_blocks++;
  245. break;
  246. case JFFS2_COMPR_ZERO:
  247. memset(data_out, 0, datalen);
  248. break;
  249. default:
  250. spin_lock(&jffs2_compressor_list_lock);
  251. list_for_each_entry(this, &jffs2_compressor_list, list) {
  252. if (comprtype == this->compr) {
  253. this->usecount++;
  254. spin_unlock(&jffs2_compressor_list_lock);
  255. ret = this->decompress(cdata_in, data_out, cdatalen, datalen);
  256. spin_lock(&jffs2_compressor_list_lock);
  257. if (ret) {
  258. printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
  259. }
  260. else {
  261. this->stat_decompr_blocks++;
  262. }
  263. this->usecount--;
  264. spin_unlock(&jffs2_compressor_list_lock);
  265. return ret;
  266. }
  267. }
  268. printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
  269. spin_unlock(&jffs2_compressor_list_lock);
  270. return -EIO;
  271. }
  272. return 0;
  273. }
  274. int jffs2_register_compressor(struct jffs2_compressor *comp)
  275. {
  276. struct jffs2_compressor *this;
  277. if (!comp->name) {
  278. printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
  279. return -1;
  280. }
  281. comp->compr_buf_size=0;
  282. comp->compr_buf=NULL;
  283. comp->usecount=0;
  284. comp->stat_compr_orig_size=0;
  285. comp->stat_compr_new_size=0;
  286. comp->stat_compr_blocks=0;
  287. comp->stat_decompr_blocks=0;
  288. D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
  289. spin_lock(&jffs2_compressor_list_lock);
  290. list_for_each_entry(this, &jffs2_compressor_list, list) {
  291. if (this->priority < comp->priority) {
  292. list_add(&comp->list, this->list.prev);
  293. goto out;
  294. }
  295. }
  296. list_add_tail(&comp->list, &jffs2_compressor_list);
  297. out:
  298. D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
  299. printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
  300. })
  301. spin_unlock(&jffs2_compressor_list_lock);
  302. return 0;
  303. }
  304. int jffs2_unregister_compressor(struct jffs2_compressor *comp)
  305. {
  306. D2(struct jffs2_compressor *this;)
  307. D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
  308. spin_lock(&jffs2_compressor_list_lock);
  309. if (comp->usecount) {
  310. spin_unlock(&jffs2_compressor_list_lock);
  311. printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
  312. return -1;
  313. }
  314. list_del(&comp->list);
  315. D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
  316. printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
  317. })
  318. spin_unlock(&jffs2_compressor_list_lock);
  319. return 0;
  320. }
  321. void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
  322. {
  323. if (orig != comprbuf)
  324. kfree(comprbuf);
  325. }
  326. int __init jffs2_compressors_init(void)
  327. {
  328. /* Registering compressors */
  329. #ifdef CONFIG_JFFS2_ZLIB
  330. jffs2_zlib_init();
  331. #endif
  332. #ifdef CONFIG_JFFS2_RTIME
  333. jffs2_rtime_init();
  334. #endif
  335. #ifdef CONFIG_JFFS2_RUBIN
  336. jffs2_rubinmips_init();
  337. jffs2_dynrubin_init();
  338. #endif
  339. #ifdef CONFIG_JFFS2_LZO
  340. jffs2_lzo_init();
  341. #endif
  342. /* Setting default compression mode */
  343. #ifdef CONFIG_JFFS2_CMODE_NONE
  344. jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
  345. D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
  346. #else
  347. #ifdef CONFIG_JFFS2_CMODE_SIZE
  348. jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
  349. D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
  350. #else
  351. #ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
  352. jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
  353. D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
  354. #else
  355. D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
  356. #endif
  357. #endif
  358. #endif
  359. return 0;
  360. }
  361. int jffs2_compressors_exit(void)
  362. {
  363. /* Unregistering compressors */
  364. #ifdef CONFIG_JFFS2_LZO
  365. jffs2_lzo_exit();
  366. #endif
  367. #ifdef CONFIG_JFFS2_RUBIN
  368. jffs2_dynrubin_exit();
  369. jffs2_rubinmips_exit();
  370. #endif
  371. #ifdef CONFIG_JFFS2_RTIME
  372. jffs2_rtime_exit();
  373. #endif
  374. #ifdef CONFIG_JFFS2_ZLIB
  375. jffs2_zlib_exit();
  376. #endif
  377. return 0;
  378. }