1 /* 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 20 static void *lzo_mem; 21 static void *lzo_compress_buf; 22 static DEFINE_MUTEX(deflate_mutex); 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) { 36 printk(KERN_WARNING "Failed to allocate lzo deflate workspace\n"); 37 free_workspace(); 38 return -ENOMEM; 39 } 40 41 return 0; 42 } 43 44 static 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); 52 mutex_unlock(&deflate_mutex); 53 54 if (ret != LZO_E_OK) 55 return -1; 56 57 if (compress_size > *dstlen) 58 return -1; 59 60 memcpy(cpage_out, lzo_compress_buf, compress_size); 61 *dstlen = compress_size; 62 63 return 0; 64 } 65 66 static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out, 67 uint32_t srclen, uint32_t destlen, void *model) 68 { 69 size_t dl = destlen; 70 int ret; 71 72 ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl); 73 74 if (ret != LZO_E_OK || dl != destlen) 75 return -1; 76 77 return 0; 78 } 79 80 static struct jffs2_compressor jffs2_lzo_comp = { 81 .priority = JFFS2_LZO_PRIORITY, 82 .name = "lzo", 83 .compr = JFFS2_COMPR_LZO, 84 .compress = &jffs2_lzo_compress, 85 .decompress = &jffs2_lzo_decompress, 86 .disabled = 0, 87 }; 88 89 int __init jffs2_lzo_init(void) 90 { 91 int ret; 92 93 ret = alloc_workspace(); 94 if (ret < 0) 95 return ret; 96 97 ret = jffs2_register_compressor(&jffs2_lzo_comp); 98 if (ret) 99 free_workspace(); 100 101 return ret; 102 } 103 104 void jffs2_lzo_exit(void) 105 { 106 jffs2_unregister_compressor(&jffs2_lzo_comp); 107 free_workspace(); 108 } 109