xref: /linux/fs/jffs2/compr_lzo.c (revision 088bd455c954c0c42edde9d4463e44be10101aac)
1c799aca3SRichard Purdie /*
2c799aca3SRichard Purdie  * JFFS2 -- Journalling Flash File System, Version 2.
3c799aca3SRichard Purdie  *
4c799aca3SRichard Purdie  * Copyright © 2007 Nokia Corporation. All rights reserved.
56088c058SDavid Woodhouse  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6c799aca3SRichard Purdie  *
7c799aca3SRichard Purdie  * Created by Richard Purdie <rpurdie@openedhand.com>
8c799aca3SRichard Purdie  *
9c799aca3SRichard Purdie  * For licensing information, see the file 'LICENCE' in this directory.
10c799aca3SRichard Purdie  *
11c799aca3SRichard Purdie  */
12c799aca3SRichard Purdie 
13c799aca3SRichard Purdie #include <linux/kernel.h>
14c799aca3SRichard Purdie #include <linux/sched.h>
15c799aca3SRichard Purdie #include <linux/vmalloc.h>
16c799aca3SRichard Purdie #include <linux/init.h>
17c799aca3SRichard Purdie #include <linux/lzo.h>
18c799aca3SRichard Purdie #include "compr.h"
19c799aca3SRichard Purdie 
20c799aca3SRichard Purdie static void *lzo_mem;
21c799aca3SRichard Purdie static void *lzo_compress_buf;
22dc8a0843SGeert Uytterhoeven static DEFINE_MUTEX(deflate_mutex);	/* for lzo_mem and lzo_compress_buf */
23c799aca3SRichard Purdie 
24c799aca3SRichard Purdie static void free_workspace(void)
25c799aca3SRichard Purdie {
26c799aca3SRichard Purdie 	vfree(lzo_mem);
27c799aca3SRichard Purdie 	vfree(lzo_compress_buf);
28c799aca3SRichard Purdie }
29c799aca3SRichard Purdie 
30c799aca3SRichard Purdie static int __init alloc_workspace(void)
31c799aca3SRichard Purdie {
32c799aca3SRichard Purdie 	lzo_mem = vmalloc(LZO1X_MEM_COMPRESS);
33c799aca3SRichard Purdie 	lzo_compress_buf = vmalloc(lzo1x_worst_compress(PAGE_SIZE));
34c799aca3SRichard Purdie 
35c799aca3SRichard Purdie 	if (!lzo_mem || !lzo_compress_buf) {
36c799aca3SRichard Purdie 		printk(KERN_WARNING "Failed to allocate lzo deflate workspace\n");
37c799aca3SRichard Purdie 		free_workspace();
38c799aca3SRichard Purdie 		return -ENOMEM;
39c799aca3SRichard Purdie 	}
40c799aca3SRichard Purdie 
41c799aca3SRichard Purdie 	return 0;
42c799aca3SRichard Purdie }
43c799aca3SRichard Purdie 
44c799aca3SRichard Purdie static int jffs2_lzo_compress(unsigned char *data_in, unsigned char *cpage_out,
45*088bd455SMike Frysinger 			      uint32_t *sourcelen, uint32_t *dstlen)
46c799aca3SRichard Purdie {
47c799aca3SRichard Purdie 	size_t compress_size;
48c799aca3SRichard Purdie 	int ret;
49c799aca3SRichard Purdie 
50c799aca3SRichard Purdie 	mutex_lock(&deflate_mutex);
51c799aca3SRichard Purdie 	ret = lzo1x_1_compress(data_in, *sourcelen, lzo_compress_buf, &compress_size, lzo_mem);
52c799aca3SRichard Purdie 	if (ret != LZO_E_OK)
53dc8a0843SGeert Uytterhoeven 		goto fail;
54c799aca3SRichard Purdie 
55c799aca3SRichard Purdie 	if (compress_size > *dstlen)
56dc8a0843SGeert Uytterhoeven 		goto fail;
57c799aca3SRichard Purdie 
58c799aca3SRichard Purdie 	memcpy(cpage_out, lzo_compress_buf, compress_size);
59dc8a0843SGeert Uytterhoeven 	mutex_unlock(&deflate_mutex);
60c799aca3SRichard Purdie 
61dc8a0843SGeert Uytterhoeven 	*dstlen = compress_size;
62c799aca3SRichard Purdie 	return 0;
63dc8a0843SGeert Uytterhoeven 
64dc8a0843SGeert Uytterhoeven  fail:
65dc8a0843SGeert Uytterhoeven 	mutex_unlock(&deflate_mutex);
66dc8a0843SGeert Uytterhoeven 	return -1;
67c799aca3SRichard Purdie }
68c799aca3SRichard Purdie 
69c799aca3SRichard Purdie static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
70*088bd455SMike Frysinger 				 uint32_t srclen, uint32_t destlen)
71c799aca3SRichard Purdie {
72c799aca3SRichard Purdie 	size_t dl = destlen;
73c799aca3SRichard Purdie 	int ret;
74c799aca3SRichard Purdie 
75c799aca3SRichard Purdie 	ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl);
76c799aca3SRichard Purdie 
77c799aca3SRichard Purdie 	if (ret != LZO_E_OK || dl != destlen)
78c799aca3SRichard Purdie 		return -1;
79c799aca3SRichard Purdie 
80c799aca3SRichard Purdie 	return 0;
81c799aca3SRichard Purdie }
82c799aca3SRichard Purdie 
83c799aca3SRichard Purdie static struct jffs2_compressor jffs2_lzo_comp = {
84c799aca3SRichard Purdie 	.priority = JFFS2_LZO_PRIORITY,
85c799aca3SRichard Purdie 	.name = "lzo",
86c799aca3SRichard Purdie 	.compr = JFFS2_COMPR_LZO,
87c799aca3SRichard Purdie 	.compress = &jffs2_lzo_compress,
88c799aca3SRichard Purdie 	.decompress = &jffs2_lzo_decompress,
89c799aca3SRichard Purdie 	.disabled = 0,
90c799aca3SRichard Purdie };
91c799aca3SRichard Purdie 
92c799aca3SRichard Purdie int __init jffs2_lzo_init(void)
93c799aca3SRichard Purdie {
94c799aca3SRichard Purdie 	int ret;
95c799aca3SRichard Purdie 
96c799aca3SRichard Purdie 	ret = alloc_workspace();
97c799aca3SRichard Purdie 	if (ret < 0)
98c799aca3SRichard Purdie 		return ret;
99c799aca3SRichard Purdie 
100c799aca3SRichard Purdie 	ret = jffs2_register_compressor(&jffs2_lzo_comp);
101c799aca3SRichard Purdie 	if (ret)
102c799aca3SRichard Purdie 		free_workspace();
103c799aca3SRichard Purdie 
104c799aca3SRichard Purdie 	return ret;
105c799aca3SRichard Purdie }
106c799aca3SRichard Purdie 
107c799aca3SRichard Purdie void jffs2_lzo_exit(void)
108c799aca3SRichard Purdie {
109c799aca3SRichard Purdie 	jffs2_unregister_compressor(&jffs2_lzo_comp);
110c799aca3SRichard Purdie 	free_workspace();
111c799aca3SRichard Purdie }
112