11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2.
31da177e4SLinus Torvalds *
4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc.
56088c058SDavid Woodhouse * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org>
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory.
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds */
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds #if !defined(__KERNEL__) && !defined(__ECOS)
141da177e4SLinus Torvalds #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
151da177e4SLinus Torvalds #endif
161da177e4SLinus Torvalds
175a528957SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
185a528957SJoe Perches
191da177e4SLinus Torvalds #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/zlib.h>
211da177e4SLinus Torvalds #include <linux/zutil.h>
221da177e4SLinus Torvalds #include "nodelist.h"
231da177e4SLinus Torvalds #include "compr.h"
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds /* Plan: call deflate() with avail_in == *sourcelen,
261da177e4SLinus Torvalds avail_out = *dstlen - 12 and flush == Z_FINISH.
271da177e4SLinus Torvalds If it doesn't manage to finish, call it again with
281da177e4SLinus Torvalds avail_in == 0 and avail_out set to the remaining 12
291da177e4SLinus Torvalds bytes for it to clean up.
301da177e4SLinus Torvalds Q: Is 12 bytes sufficient?
311da177e4SLinus Torvalds */
321da177e4SLinus Torvalds #define STREAM_END_SPACE 12
331da177e4SLinus Torvalds
34353ab6e9SIngo Molnar static DEFINE_MUTEX(deflate_mutex);
35353ab6e9SIngo Molnar static DEFINE_MUTEX(inflate_mutex);
361da177e4SLinus Torvalds static z_stream inf_strm, def_strm;
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds #ifdef __KERNEL__ /* Linux-only */
391da177e4SLinus Torvalds #include <linux/vmalloc.h>
401da177e4SLinus Torvalds #include <linux/init.h>
41353ab6e9SIngo Molnar #include <linux/mutex.h>
421da177e4SLinus Torvalds
alloc_workspaces(void)431da177e4SLinus Torvalds static int __init alloc_workspaces(void)
441da177e4SLinus Torvalds {
45565d76cbSJim Keniston def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
46565d76cbSJim Keniston MAX_MEM_LEVEL));
47045ead34SJoe Perches if (!def_strm.workspace)
481da177e4SLinus Torvalds return -ENOMEM;
49045ead34SJoe Perches
509c261b33SJoe Perches jffs2_dbg(1, "Allocated %d bytes for deflate workspace\n",
519c261b33SJoe Perches zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
521da177e4SLinus Torvalds inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
531da177e4SLinus Torvalds if (!inf_strm.workspace) {
541da177e4SLinus Torvalds vfree(def_strm.workspace);
551da177e4SLinus Torvalds return -ENOMEM;
561da177e4SLinus Torvalds }
579c261b33SJoe Perches jffs2_dbg(1, "Allocated %d bytes for inflate workspace\n",
589c261b33SJoe Perches zlib_inflate_workspacesize());
591da177e4SLinus Torvalds return 0;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds
free_workspaces(void)621da177e4SLinus Torvalds static void free_workspaces(void)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds vfree(def_strm.workspace);
651da177e4SLinus Torvalds vfree(inf_strm.workspace);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds #else
681da177e4SLinus Torvalds #define alloc_workspaces() (0)
691da177e4SLinus Torvalds #define free_workspaces() do { } while(0)
701da177e4SLinus Torvalds #endif /* __KERNEL__ */
711da177e4SLinus Torvalds
jffs2_zlib_compress(unsigned char * data_in,unsigned char * cpage_out,uint32_t * sourcelen,uint32_t * dstlen)7275c96f85SAdrian Bunk static int jffs2_zlib_compress(unsigned char *data_in,
7375c96f85SAdrian Bunk unsigned char *cpage_out,
74088bd455SMike Frysinger uint32_t *sourcelen, uint32_t *dstlen)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds int ret;
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds if (*dstlen <= STREAM_END_SPACE)
791da177e4SLinus Torvalds return -1;
801da177e4SLinus Torvalds
81353ab6e9SIngo Molnar mutex_lock(&deflate_mutex);
821da177e4SLinus Torvalds
831da177e4SLinus Torvalds if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
84da320f05SJoe Perches pr_warn("deflateInit failed\n");
85353ab6e9SIngo Molnar mutex_unlock(&deflate_mutex);
861da177e4SLinus Torvalds return -1;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds def_strm.next_in = data_in;
901da177e4SLinus Torvalds def_strm.total_in = 0;
911da177e4SLinus Torvalds
921da177e4SLinus Torvalds def_strm.next_out = cpage_out;
931da177e4SLinus Torvalds def_strm.total_out = 0;
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
961da177e4SLinus Torvalds def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
97*d97b07c5SYinghai Lu def_strm.avail_in = min_t(unsigned long,
98*d97b07c5SYinghai Lu (*sourcelen-def_strm.total_in), def_strm.avail_out);
99*d97b07c5SYinghai Lu jffs2_dbg(1, "calling deflate with avail_in %ld, avail_out %ld\n",
1009c261b33SJoe Perches def_strm.avail_in, def_strm.avail_out);
1011da177e4SLinus Torvalds ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
102*d97b07c5SYinghai Lu jffs2_dbg(1, "deflate returned with avail_in %ld, avail_out %ld, total_in %ld, total_out %ld\n",
1039c261b33SJoe Perches def_strm.avail_in, def_strm.avail_out,
1049c261b33SJoe Perches def_strm.total_in, def_strm.total_out);
1051da177e4SLinus Torvalds if (ret != Z_OK) {
1069c261b33SJoe Perches jffs2_dbg(1, "deflate in loop returned %d\n", ret);
1071da177e4SLinus Torvalds zlib_deflateEnd(&def_strm);
108353ab6e9SIngo Molnar mutex_unlock(&deflate_mutex);
1091da177e4SLinus Torvalds return -1;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds def_strm.avail_out += STREAM_END_SPACE;
1131da177e4SLinus Torvalds def_strm.avail_in = 0;
1141da177e4SLinus Torvalds ret = zlib_deflate(&def_strm, Z_FINISH);
1151da177e4SLinus Torvalds zlib_deflateEnd(&def_strm);
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds if (ret != Z_STREAM_END) {
1189c261b33SJoe Perches jffs2_dbg(1, "final deflate returned %d\n", ret);
1191da177e4SLinus Torvalds ret = -1;
1201da177e4SLinus Torvalds goto out;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds
1231da177e4SLinus Torvalds if (def_strm.total_out >= def_strm.total_in) {
1249c261b33SJoe Perches jffs2_dbg(1, "zlib compressed %ld bytes into %ld; failing\n",
1259c261b33SJoe Perches def_strm.total_in, def_strm.total_out);
1261da177e4SLinus Torvalds ret = -1;
1271da177e4SLinus Torvalds goto out;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds
1309c261b33SJoe Perches jffs2_dbg(1, "zlib compressed %ld bytes into %ld\n",
1319c261b33SJoe Perches def_strm.total_in, def_strm.total_out);
1321da177e4SLinus Torvalds
1331da177e4SLinus Torvalds *dstlen = def_strm.total_out;
1341da177e4SLinus Torvalds *sourcelen = def_strm.total_in;
1351da177e4SLinus Torvalds ret = 0;
1361da177e4SLinus Torvalds out:
137353ab6e9SIngo Molnar mutex_unlock(&deflate_mutex);
1381da177e4SLinus Torvalds return ret;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds
jffs2_zlib_decompress(unsigned char * data_in,unsigned char * cpage_out,uint32_t srclen,uint32_t destlen)14175c96f85SAdrian Bunk static int jffs2_zlib_decompress(unsigned char *data_in,
14275c96f85SAdrian Bunk unsigned char *cpage_out,
143088bd455SMike Frysinger uint32_t srclen, uint32_t destlen)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds int ret;
1461da177e4SLinus Torvalds int wbits = MAX_WBITS;
1471da177e4SLinus Torvalds
148353ab6e9SIngo Molnar mutex_lock(&inflate_mutex);
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds inf_strm.next_in = data_in;
1511da177e4SLinus Torvalds inf_strm.avail_in = srclen;
1521da177e4SLinus Torvalds inf_strm.total_in = 0;
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds inf_strm.next_out = cpage_out;
1551da177e4SLinus Torvalds inf_strm.avail_out = destlen;
1561da177e4SLinus Torvalds inf_strm.total_out = 0;
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds /* If it's deflate, and it's got no preset dictionary, then
1591da177e4SLinus Torvalds we can tell zlib to skip the adler32 check. */
1601da177e4SLinus Torvalds if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
1611da177e4SLinus Torvalds ((data_in[0] & 0x0f) == Z_DEFLATED) &&
1621da177e4SLinus Torvalds !(((data_in[0]<<8) + data_in[1]) % 31)) {
1631da177e4SLinus Torvalds
1649c261b33SJoe Perches jffs2_dbg(2, "inflate skipping adler32\n");
1651da177e4SLinus Torvalds wbits = -((data_in[0] >> 4) + 8);
1661da177e4SLinus Torvalds inf_strm.next_in += 2;
1671da177e4SLinus Torvalds inf_strm.avail_in -= 2;
1681da177e4SLinus Torvalds } else {
1691da177e4SLinus Torvalds /* Let this remain D1 for now -- it should never happen */
1709c261b33SJoe Perches jffs2_dbg(1, "inflate not skipping adler32\n");
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds
1731da177e4SLinus Torvalds
1741da177e4SLinus Torvalds if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
175da320f05SJoe Perches pr_warn("inflateInit failed\n");
176353ab6e9SIngo Molnar mutex_unlock(&inflate_mutex);
1771da177e4SLinus Torvalds return 1;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds
1801da177e4SLinus Torvalds while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
1811da177e4SLinus Torvalds ;
1821da177e4SLinus Torvalds if (ret != Z_STREAM_END) {
183da320f05SJoe Perches pr_notice("inflate returned %d\n", ret);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds zlib_inflateEnd(&inf_strm);
186353ab6e9SIngo Molnar mutex_unlock(&inflate_mutex);
1871da177e4SLinus Torvalds return 0;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds
1901da177e4SLinus Torvalds static struct jffs2_compressor jffs2_zlib_comp = {
1911da177e4SLinus Torvalds .priority = JFFS2_ZLIB_PRIORITY,
1921da177e4SLinus Torvalds .name = "zlib",
1931da177e4SLinus Torvalds .compr = JFFS2_COMPR_ZLIB,
1941da177e4SLinus Torvalds .compress = &jffs2_zlib_compress,
1951da177e4SLinus Torvalds .decompress = &jffs2_zlib_decompress,
1961da177e4SLinus Torvalds #ifdef JFFS2_ZLIB_DISABLED
1971da177e4SLinus Torvalds .disabled = 1,
1981da177e4SLinus Torvalds #else
1991da177e4SLinus Torvalds .disabled = 0,
2001da177e4SLinus Torvalds #endif
2011da177e4SLinus Torvalds };
2021da177e4SLinus Torvalds
jffs2_zlib_init(void)2031da177e4SLinus Torvalds int __init jffs2_zlib_init(void)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds int ret;
2061da177e4SLinus Torvalds
2071da177e4SLinus Torvalds ret = alloc_workspaces();
2081da177e4SLinus Torvalds if (ret)
2091da177e4SLinus Torvalds return ret;
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds ret = jffs2_register_compressor(&jffs2_zlib_comp);
2121da177e4SLinus Torvalds if (ret)
2131da177e4SLinus Torvalds free_workspaces();
2141da177e4SLinus Torvalds
2151da177e4SLinus Torvalds return ret;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds
jffs2_zlib_exit(void)2181da177e4SLinus Torvalds void jffs2_zlib_exit(void)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds jffs2_unregister_compressor(&jffs2_zlib_comp);
2211da177e4SLinus Torvalds free_workspaces();
2221da177e4SLinus Torvalds }
223