compr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
  5. * University of Szeged, Hungary
  6. *
  7. * For licensing information, see the file 'LICENCE' in this directory
  8. * in the jffs2 directory.
  9. */
  10. #include "compr.h"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <linux/jffs2.h>
  14. #define FAVOUR_LZO_PERCENT 80
  15. extern int page_size;
  16. /* LIST IMPLEMENTATION (from linux/list.h) */
  17. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  18. #define LIST_HEAD(name) \
  19. struct list_head name = LIST_HEAD_INIT(name)
  20. static inline void __list_add(struct list_head *new,
  21. struct list_head *prev,
  22. struct list_head *next)
  23. {
  24. next->prev = new;
  25. new->next = next;
  26. new->prev = prev;
  27. prev->next = new;
  28. }
  29. static inline void list_add(struct list_head *new, struct list_head *head)
  30. {
  31. __list_add(new, head, head->next);
  32. }
  33. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  34. {
  35. __list_add(new, head->prev, head);
  36. }
  37. static inline void __list_del(struct list_head *prev, struct list_head *next)
  38. {
  39. next->prev = prev;
  40. prev->next = next;
  41. }
  42. static inline void list_del(struct list_head *entry)
  43. {
  44. __list_del(entry->prev, entry->next);
  45. entry->next = (void *) 0;
  46. entry->prev = (void *) 0;
  47. }
  48. #define list_entry(ptr, type, member) \
  49. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  50. #define list_for_each_entry(pos, head, member) \
  51. for (pos = list_entry((head)->next, typeof(*pos), member); \
  52. &pos->member != (head); \
  53. pos = list_entry(pos->member.next, typeof(*pos), member))
  54. /* Available compressors are on this list */
  55. static LIST_HEAD(jffs2_compressor_list);
  56. /* Actual compression mode */
  57. static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
  58. void jffs2_set_compression_mode(int mode)
  59. {
  60. jffs2_compression_mode = mode;
  61. }
  62. int jffs2_get_compression_mode(void)
  63. {
  64. return jffs2_compression_mode;
  65. }
  66. /* Statistics for blocks stored without compression */
  67. static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
  68. /* Compression test stuffs */
  69. static int jffs2_compression_check = 0;
  70. static unsigned char *jffs2_compression_check_buf = NULL;
  71. void jffs2_compression_check_set(int yesno)
  72. {
  73. jffs2_compression_check = yesno;
  74. }
  75. int jffs2_compression_check_get(void)
  76. {
  77. return jffs2_compression_check;
  78. }
  79. static int jffs2_error_cnt = 0;
  80. int jffs2_compression_check_errorcnt_get(void)
  81. {
  82. return jffs2_error_cnt;
  83. }
  84. #define JFFS2_BUFFER_FILL 0x55
  85. /* Called before compression (if compression_check is setted) to prepare
  86. the buffer for buffer overflow test */
  87. static void jffs2_decompression_test_prepare(unsigned char *buf, int size)
  88. {
  89. memset(buf,JFFS2_BUFFER_FILL,size+1);
  90. }
  91. /* Called after compression (if compression_check is setted) to test the result */
  92. static void jffs2_decompression_test(struct jffs2_compressor *compr,
  93. unsigned char *data_in, unsigned char *output_buf,
  94. uint32_t cdatalen, uint32_t datalen, uint32_t buf_size)
  95. {
  96. uint32_t i;
  97. /* buffer overflow test */
  98. for (i=buf_size;i>cdatalen;i--) {
  99. if (output_buf[i]!=JFFS2_BUFFER_FILL) {
  100. fprintf(stderr,"COMPR_ERROR: buffer overflow at %s. "
  101. "(bs=%d csize=%d b[%d]=%d)\n", compr->name,
  102. buf_size, cdatalen, i, (int)(output_buf[i]));
  103. jffs2_error_cnt++;
  104. return;
  105. }
  106. }
  107. /* allocing temporary buffer for decompression */
  108. if (!jffs2_compression_check_buf) {
  109. jffs2_compression_check_buf = malloc(page_size);
  110. if (!jffs2_compression_check_buf) {
  111. fprintf(stderr,"No memory for buffer allocation. Compression check disabled.\n");
  112. jffs2_compression_check = 0;
  113. return;
  114. }
  115. }
  116. /* decompressing */
  117. if (!compr->decompress) {
  118. fprintf(stderr,"JFFS2 compression check: there is no decompress function at %s.\n", compr->name);
  119. jffs2_error_cnt++;
  120. return;
  121. }
  122. if (compr->decompress(output_buf,jffs2_compression_check_buf,cdatalen,datalen)) {
  123. fprintf(stderr,"JFFS2 compression check: decompression failed at %s.\n", compr->name);
  124. jffs2_error_cnt++;
  125. }
  126. /* validate decompression */
  127. else {
  128. for (i=0;i<datalen;i++) {
  129. if (data_in[i]!=jffs2_compression_check_buf[i]) {
  130. fprintf(stderr,"JFFS2 compression check: data mismatch at %s (pos %d).\n", compr->name, i);
  131. jffs2_error_cnt++;
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. /*
  138. * Return 1 to use this compression
  139. */
  140. static int jffs2_is_best_compression(struct jffs2_compressor *this,
  141. struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
  142. {
  143. switch (jffs2_compression_mode) {
  144. case JFFS2_COMPR_MODE_SIZE:
  145. if (bestsize > size)
  146. return 1;
  147. return 0;
  148. case JFFS2_COMPR_MODE_FAVOURLZO:
  149. if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
  150. return 1;
  151. if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
  152. return 1;
  153. if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
  154. return 1;
  155. if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
  156. return 1;
  157. return 0;
  158. }
  159. /* Shouldn't happen */
  160. return 0;
  161. }
  162. /* jffs2_compress:
  163. * @data: Pointer to uncompressed data
  164. * @cdata: Pointer to returned pointer to buffer for compressed data
  165. * @datalen: On entry, holds the amount of data available for compression.
  166. * On exit, expected to hold the amount of data actually compressed.
  167. * @cdatalen: On entry, holds the amount of space available for compressed
  168. * data. On exit, expected to hold the actual size of the compressed
  169. * data.
  170. *
  171. * Returns: Lower byte to be stored with data indicating compression type used.
  172. * Zero is used to show that the data could not be compressed - the
  173. * compressed version was actually larger than the original.
  174. * Upper byte will be used later. (soon)
  175. *
  176. * If the cdata buffer isn't large enough to hold all the uncompressed data,
  177. * jffs2_compress should compress as much as will fit, and should set
  178. * *datalen accordingly to show the amount of data which were compressed.
  179. */
  180. uint16_t jffs2_compress( unsigned char *data_in, unsigned char **cpage_out,
  181. uint32_t *datalen, uint32_t *cdatalen)
  182. {
  183. int ret = JFFS2_COMPR_NONE;
  184. int compr_ret;
  185. struct jffs2_compressor *this, *best=NULL;
  186. unsigned char *output_buf = NULL, *tmp_buf;
  187. uint32_t orig_slen, orig_dlen;
  188. uint32_t best_slen=0, best_dlen=0;
  189. switch (jffs2_compression_mode) {
  190. case JFFS2_COMPR_MODE_NONE:
  191. break;
  192. case JFFS2_COMPR_MODE_PRIORITY:
  193. orig_slen = *datalen;
  194. orig_dlen = *cdatalen;
  195. output_buf = malloc(orig_dlen+jffs2_compression_check);
  196. if (!output_buf) {
  197. fprintf(stderr,"mkfs.jffs2: No memory for compressor allocation. Compression failed.\n");
  198. goto out;
  199. }
  200. list_for_each_entry(this, &jffs2_compressor_list, list) {
  201. /* Skip decompress-only backwards-compatibility and disabled modules */
  202. if ((!this->compress)||(this->disabled))
  203. continue;
  204. this->usecount++;
  205. if (jffs2_compression_check) /*preparing output buffer for testing buffer overflow */
  206. jffs2_decompression_test_prepare(output_buf, orig_dlen);
  207. *datalen = orig_slen;
  208. *cdatalen = orig_dlen;
  209. compr_ret = this->compress(data_in, output_buf, datalen, cdatalen);
  210. this->usecount--;
  211. if (!compr_ret) {
  212. ret = this->compr;
  213. this->stat_compr_blocks++;
  214. this->stat_compr_orig_size += *datalen;
  215. this->stat_compr_new_size += *cdatalen;
  216. if (jffs2_compression_check)
  217. jffs2_decompression_test(this, data_in, output_buf, *cdatalen, *datalen, orig_dlen);
  218. break;
  219. }
  220. }
  221. if (ret == JFFS2_COMPR_NONE) free(output_buf);
  222. break;
  223. case JFFS2_COMPR_MODE_FAVOURLZO:
  224. case JFFS2_COMPR_MODE_SIZE:
  225. orig_slen = *datalen;
  226. orig_dlen = *cdatalen;
  227. list_for_each_entry(this, &jffs2_compressor_list, list) {
  228. uint32_t needed_buf_size;
  229. if (jffs2_compression_mode == JFFS2_COMPR_MODE_FAVOURLZO)
  230. needed_buf_size = orig_slen + jffs2_compression_check;
  231. else
  232. needed_buf_size = orig_dlen + jffs2_compression_check;
  233. /* Skip decompress-only backwards-compatibility and disabled modules */
  234. if ((!this->compress)||(this->disabled))
  235. continue;
  236. /* Allocating memory for output buffer if necessary */
  237. if ((this->compr_buf_size < needed_buf_size) && (this->compr_buf)) {
  238. free(this->compr_buf);
  239. this->compr_buf_size=0;
  240. this->compr_buf=NULL;
  241. }
  242. if (!this->compr_buf) {
  243. tmp_buf = malloc(needed_buf_size);
  244. if (!tmp_buf) {
  245. fprintf(stderr,"mkfs.jffs2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
  246. continue;
  247. }
  248. else {
  249. this->compr_buf = tmp_buf;
  250. this->compr_buf_size = orig_dlen;
  251. }
  252. }
  253. this->usecount++;
  254. if (jffs2_compression_check) /*preparing output buffer for testing buffer overflow */
  255. jffs2_decompression_test_prepare(this->compr_buf,this->compr_buf_size);
  256. *datalen = orig_slen;
  257. *cdatalen = orig_dlen;
  258. compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
  259. this->usecount--;
  260. if (!compr_ret) {
  261. if (jffs2_compression_check)
  262. jffs2_decompression_test(this, data_in, this->compr_buf, *cdatalen, *datalen, this->compr_buf_size);
  263. if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
  264. && (*cdatalen < *datalen)) {
  265. best_dlen = *cdatalen;
  266. best_slen = *datalen;
  267. best = this;
  268. }
  269. }
  270. }
  271. if (best_dlen) {
  272. *cdatalen = best_dlen;
  273. *datalen = best_slen;
  274. output_buf = best->compr_buf;
  275. best->compr_buf = NULL;
  276. best->compr_buf_size = 0;
  277. best->stat_compr_blocks++;
  278. best->stat_compr_orig_size += best_slen;
  279. best->stat_compr_new_size += best_dlen;
  280. ret = best->compr;
  281. }
  282. break;
  283. default:
  284. fprintf(stderr,"mkfs.jffs2: unknown compression mode.\n");
  285. }
  286. out:
  287. if (ret == JFFS2_COMPR_NONE) {
  288. *cpage_out = data_in;
  289. *datalen = *cdatalen;
  290. none_stat_compr_blocks++;
  291. none_stat_compr_size += *datalen;
  292. }
  293. else {
  294. *cpage_out = output_buf;
  295. }
  296. return ret;
  297. }
  298. int jffs2_register_compressor(struct jffs2_compressor *comp)
  299. {
  300. struct jffs2_compressor *this;
  301. if (!comp->name) {
  302. fprintf(stderr,"NULL compressor name at registering JFFS2 compressor. Failed.\n");
  303. return -1;
  304. }
  305. comp->compr_buf_size=0;
  306. comp->compr_buf=NULL;
  307. comp->usecount=0;
  308. comp->stat_compr_orig_size=0;
  309. comp->stat_compr_new_size=0;
  310. comp->stat_compr_blocks=0;
  311. comp->stat_decompr_blocks=0;
  312. list_for_each_entry(this, &jffs2_compressor_list, list) {
  313. if (this->priority < comp->priority) {
  314. list_add(&comp->list, this->list.prev);
  315. goto out;
  316. }
  317. }
  318. list_add_tail(&comp->list, &jffs2_compressor_list);
  319. out:
  320. return 0;
  321. }
  322. int jffs2_unregister_compressor(struct jffs2_compressor *comp)
  323. {
  324. if (comp->usecount) {
  325. fprintf(stderr,"mkfs.jffs2: Compressor modul is in use. Unregister failed.\n");
  326. return -1;
  327. }
  328. list_del(&comp->list);
  329. return 0;
  330. }
  331. #define JFFS2_STAT_BUF_SIZE 16000
  332. char *jffs2_list_compressors(void)
  333. {
  334. struct jffs2_compressor *this;
  335. char *buf, *act_buf;
  336. act_buf = buf = malloc(JFFS2_STAT_BUF_SIZE);
  337. list_for_each_entry(this, &jffs2_compressor_list, list) {
  338. act_buf += sprintf(act_buf, "%10s priority:%d ", this->name, this->priority);
  339. if ((this->disabled)||(!this->compress))
  340. act_buf += sprintf(act_buf,"disabled");
  341. else
  342. act_buf += sprintf(act_buf,"enabled");
  343. act_buf += sprintf(act_buf,"\n");
  344. }
  345. return buf;
  346. }
  347. char *jffs2_stats(void)
  348. {
  349. struct jffs2_compressor *this;
  350. char *buf, *act_buf;
  351. act_buf = buf = malloc(JFFS2_STAT_BUF_SIZE);
  352. act_buf += sprintf(act_buf,"Compression mode: ");
  353. switch (jffs2_compression_mode) {
  354. case JFFS2_COMPR_MODE_NONE:
  355. act_buf += sprintf(act_buf,"none");
  356. break;
  357. case JFFS2_COMPR_MODE_PRIORITY:
  358. act_buf += sprintf(act_buf,"priority");
  359. break;
  360. case JFFS2_COMPR_MODE_SIZE:
  361. act_buf += sprintf(act_buf,"size");
  362. break;
  363. case JFFS2_COMPR_MODE_FAVOURLZO:
  364. act_buf += sprintf(act_buf, "favourlzo");
  365. break;
  366. default:
  367. act_buf += sprintf(act_buf, "unknown");
  368. break;
  369. }
  370. act_buf += sprintf(act_buf,"\nCompressors:\n");
  371. act_buf += sprintf(act_buf,"%10s ","none");
  372. act_buf += sprintf(act_buf,"compr: %d blocks (%d) decompr: %d blocks\n", none_stat_compr_blocks,
  373. none_stat_compr_size, none_stat_decompr_blocks);
  374. list_for_each_entry(this, &jffs2_compressor_list, list) {
  375. act_buf += sprintf(act_buf,"%10s (prio:%d) ",this->name,this->priority);
  376. if ((this->disabled)||(!this->compress))
  377. act_buf += sprintf(act_buf,"- ");
  378. else
  379. act_buf += sprintf(act_buf,"+ ");
  380. act_buf += sprintf(act_buf,"compr: %d blocks (%d/%d) decompr: %d blocks ", this->stat_compr_blocks,
  381. this->stat_compr_new_size, this->stat_compr_orig_size,
  382. this->stat_decompr_blocks);
  383. act_buf += sprintf(act_buf,"\n");
  384. }
  385. return buf;
  386. }
  387. int jffs2_set_compression_mode_name(const char *name)
  388. {
  389. if (!strcmp("none",name)) {
  390. jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
  391. return 0;
  392. }
  393. if (!strcmp("priority",name)) {
  394. jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
  395. return 0;
  396. }
  397. if (!strcmp("size",name)) {
  398. jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
  399. return 0;
  400. }
  401. if (!strcmp("favourlzo", name)) {
  402. jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
  403. return 0;
  404. }
  405. return 1;
  406. }
  407. static int jffs2_compressor_Xable(const char *name, int disabled)
  408. {
  409. struct jffs2_compressor *this;
  410. list_for_each_entry(this, &jffs2_compressor_list, list) {
  411. if (!strcmp(this->name, name)) {
  412. this->disabled = disabled;
  413. return 0;
  414. }
  415. }
  416. return 1;
  417. }
  418. int jffs2_enable_compressor_name(const char *name)
  419. {
  420. return jffs2_compressor_Xable(name, 0);
  421. }
  422. int jffs2_disable_compressor_name(const char *name)
  423. {
  424. return jffs2_compressor_Xable(name, 1);
  425. }
  426. int jffs2_set_compressor_priority(const char *name, int priority)
  427. {
  428. struct jffs2_compressor *this,*comp;
  429. list_for_each_entry(this, &jffs2_compressor_list, list) {
  430. if (!strcmp(this->name, name)) {
  431. this->priority = priority;
  432. comp = this;
  433. goto reinsert;
  434. }
  435. }
  436. fprintf(stderr,"mkfs.jffs2: compressor %s not found.\n",name);
  437. return 1;
  438. reinsert:
  439. /* list is sorted in the order of priority, so if
  440. we change it we have to reinsert it into the
  441. good place */
  442. list_del(&comp->list);
  443. list_for_each_entry(this, &jffs2_compressor_list, list) {
  444. if (this->priority < comp->priority) {
  445. list_add(&comp->list, this->list.prev);
  446. return 0;
  447. }
  448. }
  449. list_add_tail(&comp->list, &jffs2_compressor_list);
  450. return 0;
  451. }
  452. int jffs2_compressors_init(void)
  453. {
  454. #ifdef CONFIG_JFFS2_ZLIB
  455. jffs2_zlib_init();
  456. #endif
  457. #ifdef CONFIG_JFFS2_RTIME
  458. jffs2_rtime_init();
  459. #endif
  460. #ifdef CONFIG_JFFS2_LZO
  461. jffs2_lzo_init();
  462. #endif
  463. return 0;
  464. }
  465. int jffs2_compressors_exit(void)
  466. {
  467. #ifdef CONFIG_JFFS2_RTIME
  468. jffs2_rtime_exit();
  469. #endif
  470. #ifdef CONFIG_JFFS2_ZLIB
  471. jffs2_zlib_exit();
  472. #endif
  473. #ifdef CONFIG_JFFS2_LZO
  474. jffs2_lzo_exit();
  475. #endif
  476. return 0;
  477. }