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