blob: 90cb60d0978767c305ce1ed888e712471b72b0d3 [file] [log] [blame]
Richard Purdiec799aca2007-07-10 10:28:36 +01001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright © 2007 Nokia Corporation. All rights reserved.
5 *
6 * Created by Richard Purdie <rpurdie@openedhand.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/slab.h>
15#include <linux/vmalloc.h>
16#include <linux/init.h>
17#include <linux/lzo.h>
18#include "compr.h"
19
20static void *lzo_mem;
21static void *lzo_compress_buf;
Geert Uytterhoevendc8a0842008-11-05 23:21:16 +010022static DEFINE_MUTEX(deflate_mutex); /* for lzo_mem and lzo_compress_buf */
Richard Purdiec799aca2007-07-10 10:28:36 +010023
24static void free_workspace(void)
25{
26 vfree(lzo_mem);
27 vfree(lzo_compress_buf);
28}
29
30static int __init alloc_workspace(void)
31{
32 lzo_mem = vmalloc(LZO1X_MEM_COMPRESS);
33 lzo_compress_buf = vmalloc(lzo1x_worst_compress(PAGE_SIZE));
34
35 if (!lzo_mem || !lzo_compress_buf) {
36 printk(KERN_WARNING "Failed to allocate lzo deflate workspace\n");
37 free_workspace();
38 return -ENOMEM;
39 }
40
41 return 0;
42}
43
44static int jffs2_lzo_compress(unsigned char *data_in, unsigned char *cpage_out,
45 uint32_t *sourcelen, uint32_t *dstlen, void *model)
46{
47 size_t compress_size;
48 int ret;
49
50 mutex_lock(&deflate_mutex);
51 ret = lzo1x_1_compress(data_in, *sourcelen, lzo_compress_buf, &compress_size, lzo_mem);
Richard Purdiec799aca2007-07-10 10:28:36 +010052 if (ret != LZO_E_OK)
Geert Uytterhoevendc8a0842008-11-05 23:21:16 +010053 goto fail;
Richard Purdiec799aca2007-07-10 10:28:36 +010054
55 if (compress_size > *dstlen)
Geert Uytterhoevendc8a0842008-11-05 23:21:16 +010056 goto fail;
Richard Purdiec799aca2007-07-10 10:28:36 +010057
58 memcpy(cpage_out, lzo_compress_buf, compress_size);
Geert Uytterhoevendc8a0842008-11-05 23:21:16 +010059 mutex_unlock(&deflate_mutex);
Richard Purdiec799aca2007-07-10 10:28:36 +010060
Geert Uytterhoevendc8a0842008-11-05 23:21:16 +010061 *dstlen = compress_size;
Richard Purdiec799aca2007-07-10 10:28:36 +010062 return 0;
Geert Uytterhoevendc8a0842008-11-05 23:21:16 +010063
64 fail:
65 mutex_unlock(&deflate_mutex);
66 return -1;
Richard Purdiec799aca2007-07-10 10:28:36 +010067}
68
69static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
70 uint32_t srclen, uint32_t destlen, void *model)
71{
72 size_t dl = destlen;
73 int ret;
74
75 ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl);
76
77 if (ret != LZO_E_OK || dl != destlen)
78 return -1;
79
80 return 0;
81}
82
83static struct jffs2_compressor jffs2_lzo_comp = {
84 .priority = JFFS2_LZO_PRIORITY,
85 .name = "lzo",
86 .compr = JFFS2_COMPR_LZO,
87 .compress = &jffs2_lzo_compress,
88 .decompress = &jffs2_lzo_decompress,
89 .disabled = 0,
90};
91
92int __init jffs2_lzo_init(void)
93{
94 int ret;
95
96 ret = alloc_workspace();
97 if (ret < 0)
98 return ret;
99
100 ret = jffs2_register_compressor(&jffs2_lzo_comp);
101 if (ret)
102 free_workspace();
103
104 return ret;
105}
106
107void jffs2_lzo_exit(void)
108{
109 jffs2_unregister_compressor(&jffs2_lzo_comp);
110 free_workspace();
111}