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