compr.c 12 KB

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