Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 Red Hat, Inc. |
| 5 | * |
| 6 | * Created by David Woodhouse <dwmw2@infradead.org> |
| 7 | * |
| 8 | * For licensing information, see the file 'LICENCE' in this directory. |
| 9 | * |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 10 | * $Id: compr_zlib.c,v 1.32 2005/11/07 11:14:38 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | |
| 14 | #if !defined(__KERNEL__) && !defined(__ECOS) |
| 15 | #error "The userspace support got too messy and was removed. Update your mkfs.jffs2" |
| 16 | #endif |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Thomas Gleixner | c84441c | 2005-05-20 20:30:09 +0100 | [diff] [blame] | 19 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/zlib.h> |
| 22 | #include <linux/zutil.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "nodelist.h" |
| 24 | #include "compr.h" |
| 25 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 26 | /* Plan: call deflate() with avail_in == *sourcelen, |
| 27 | avail_out = *dstlen - 12 and flush == Z_FINISH. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | If it doesn't manage to finish, call it again with |
| 29 | avail_in == 0 and avail_out set to the remaining 12 |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 30 | bytes for it to clean up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | Q: Is 12 bytes sufficient? |
| 32 | */ |
| 33 | #define STREAM_END_SPACE 12 |
| 34 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 35 | static DEFINE_MUTEX(deflate_mutex); |
| 36 | static DEFINE_MUTEX(inflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static z_stream inf_strm, def_strm; |
| 38 | |
| 39 | #ifdef __KERNEL__ /* Linux-only */ |
| 40 | #include <linux/vmalloc.h> |
| 41 | #include <linux/init.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 42 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | static int __init alloc_workspaces(void) |
| 45 | { |
| 46 | def_strm.workspace = vmalloc(zlib_deflate_workspacesize()); |
| 47 | if (!def_strm.workspace) { |
| 48 | printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize()); |
| 49 | return -ENOMEM; |
| 50 | } |
| 51 | D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize())); |
| 52 | inf_strm.workspace = vmalloc(zlib_inflate_workspacesize()); |
| 53 | if (!inf_strm.workspace) { |
| 54 | printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize()); |
| 55 | vfree(def_strm.workspace); |
| 56 | return -ENOMEM; |
| 57 | } |
| 58 | D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize())); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void free_workspaces(void) |
| 63 | { |
| 64 | vfree(def_strm.workspace); |
| 65 | vfree(inf_strm.workspace); |
| 66 | } |
| 67 | #else |
| 68 | #define alloc_workspaces() (0) |
| 69 | #define free_workspaces() do { } while(0) |
| 70 | #endif /* __KERNEL__ */ |
| 71 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 72 | static int jffs2_zlib_compress(unsigned char *data_in, |
| 73 | unsigned char *cpage_out, |
| 74 | uint32_t *sourcelen, uint32_t *dstlen, |
| 75 | void *model) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | if (*dstlen <= STREAM_END_SPACE) |
| 80 | return -1; |
| 81 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 82 | mutex_lock(&deflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | if (Z_OK != zlib_deflateInit(&def_strm, 3)) { |
| 85 | printk(KERN_WARNING "deflateInit failed\n"); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 86 | mutex_unlock(&deflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | def_strm.next_in = data_in; |
| 91 | def_strm.total_in = 0; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | def_strm.next_out = cpage_out; |
| 94 | def_strm.total_out = 0; |
| 95 | |
| 96 | while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) { |
| 97 | def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE); |
| 98 | def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out); |
| 99 | D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n", |
| 100 | def_strm.avail_in, def_strm.avail_out)); |
| 101 | ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 102 | D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out)); |
| 104 | if (ret != Z_OK) { |
| 105 | D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret)); |
| 106 | zlib_deflateEnd(&def_strm); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 107 | mutex_unlock(&deflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | def_strm.avail_out += STREAM_END_SPACE; |
| 112 | def_strm.avail_in = 0; |
| 113 | ret = zlib_deflate(&def_strm, Z_FINISH); |
| 114 | zlib_deflateEnd(&def_strm); |
| 115 | |
| 116 | if (ret != Z_STREAM_END) { |
| 117 | D1(printk(KERN_DEBUG "final deflate returned %d\n", ret)); |
| 118 | ret = -1; |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | if (def_strm.total_out >= def_strm.total_in) { |
| 123 | D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n", |
| 124 | def_strm.total_in, def_strm.total_out)); |
| 125 | ret = -1; |
| 126 | goto out; |
| 127 | } |
| 128 | |
| 129 | D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n", |
| 130 | def_strm.total_in, def_strm.total_out)); |
| 131 | |
| 132 | *dstlen = def_strm.total_out; |
| 133 | *sourcelen = def_strm.total_in; |
| 134 | ret = 0; |
| 135 | out: |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 136 | mutex_unlock(&deflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return ret; |
| 138 | } |
| 139 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 140 | static int jffs2_zlib_decompress(unsigned char *data_in, |
| 141 | unsigned char *cpage_out, |
| 142 | uint32_t srclen, uint32_t destlen, |
| 143 | void *model) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | int ret; |
| 146 | int wbits = MAX_WBITS; |
| 147 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 148 | mutex_lock(&inflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | inf_strm.next_in = data_in; |
| 151 | inf_strm.avail_in = srclen; |
| 152 | inf_strm.total_in = 0; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | inf_strm.next_out = cpage_out; |
| 155 | inf_strm.avail_out = destlen; |
| 156 | inf_strm.total_out = 0; |
| 157 | |
| 158 | /* If it's deflate, and it's got no preset dictionary, then |
| 159 | we can tell zlib to skip the adler32 check. */ |
| 160 | if (srclen > 2 && !(data_in[1] & PRESET_DICT) && |
| 161 | ((data_in[0] & 0x0f) == Z_DEFLATED) && |
| 162 | !(((data_in[0]<<8) + data_in[1]) % 31)) { |
| 163 | |
| 164 | D2(printk(KERN_DEBUG "inflate skipping adler32\n")); |
| 165 | wbits = -((data_in[0] >> 4) + 8); |
| 166 | inf_strm.next_in += 2; |
| 167 | inf_strm.avail_in -= 2; |
| 168 | } else { |
| 169 | /* Let this remain D1 for now -- it should never happen */ |
| 170 | D1(printk(KERN_DEBUG "inflate not skipping adler32\n")); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) { |
| 175 | printk(KERN_WARNING "inflateInit failed\n"); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 176 | mutex_unlock(&inflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK) |
| 181 | ; |
| 182 | if (ret != Z_STREAM_END) { |
| 183 | printk(KERN_NOTICE "inflate returned %d\n", ret); |
| 184 | } |
| 185 | zlib_inflateEnd(&inf_strm); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 186 | mutex_unlock(&inflate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static struct jffs2_compressor jffs2_zlib_comp = { |
| 191 | .priority = JFFS2_ZLIB_PRIORITY, |
| 192 | .name = "zlib", |
| 193 | .compr = JFFS2_COMPR_ZLIB, |
| 194 | .compress = &jffs2_zlib_compress, |
| 195 | .decompress = &jffs2_zlib_decompress, |
| 196 | #ifdef JFFS2_ZLIB_DISABLED |
| 197 | .disabled = 1, |
| 198 | #else |
| 199 | .disabled = 0, |
| 200 | #endif |
| 201 | }; |
| 202 | |
| 203 | int __init jffs2_zlib_init(void) |
| 204 | { |
| 205 | int ret; |
| 206 | |
| 207 | ret = alloc_workspaces(); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
| 211 | ret = jffs2_register_compressor(&jffs2_zlib_comp); |
| 212 | if (ret) |
| 213 | free_workspaces(); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | void jffs2_zlib_exit(void) |
| 219 | { |
| 220 | jffs2_unregister_compressor(&jffs2_zlib_comp); |
| 221 | free_workspaces(); |
| 222 | } |