11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4*c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #if !defined(__KERNEL__) && !defined(__ECOS) 131da177e4SLinus Torvalds #error "The userspace support got too messy and was removed. Update your mkfs.jffs2" 141da177e4SLinus Torvalds #endif 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/zlib.h> 191da177e4SLinus Torvalds #include <linux/zutil.h> 201da177e4SLinus Torvalds #include "nodelist.h" 211da177e4SLinus Torvalds #include "compr.h" 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* Plan: call deflate() with avail_in == *sourcelen, 241da177e4SLinus Torvalds avail_out = *dstlen - 12 and flush == Z_FINISH. 251da177e4SLinus Torvalds If it doesn't manage to finish, call it again with 261da177e4SLinus Torvalds avail_in == 0 and avail_out set to the remaining 12 271da177e4SLinus Torvalds bytes for it to clean up. 281da177e4SLinus Torvalds Q: Is 12 bytes sufficient? 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds #define STREAM_END_SPACE 12 311da177e4SLinus Torvalds 32353ab6e9SIngo Molnar static DEFINE_MUTEX(deflate_mutex); 33353ab6e9SIngo Molnar static DEFINE_MUTEX(inflate_mutex); 341da177e4SLinus Torvalds static z_stream inf_strm, def_strm; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #ifdef __KERNEL__ /* Linux-only */ 371da177e4SLinus Torvalds #include <linux/vmalloc.h> 381da177e4SLinus Torvalds #include <linux/init.h> 39353ab6e9SIngo Molnar #include <linux/mutex.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static int __init alloc_workspaces(void) 421da177e4SLinus Torvalds { 431da177e4SLinus Torvalds def_strm.workspace = vmalloc(zlib_deflate_workspacesize()); 441da177e4SLinus Torvalds if (!def_strm.workspace) { 451da177e4SLinus Torvalds printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize()); 461da177e4SLinus Torvalds return -ENOMEM; 471da177e4SLinus Torvalds } 481da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize())); 491da177e4SLinus Torvalds inf_strm.workspace = vmalloc(zlib_inflate_workspacesize()); 501da177e4SLinus Torvalds if (!inf_strm.workspace) { 511da177e4SLinus Torvalds printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize()); 521da177e4SLinus Torvalds vfree(def_strm.workspace); 531da177e4SLinus Torvalds return -ENOMEM; 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize())); 561da177e4SLinus Torvalds return 0; 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds static void free_workspaces(void) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds vfree(def_strm.workspace); 621da177e4SLinus Torvalds vfree(inf_strm.workspace); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds #else 651da177e4SLinus Torvalds #define alloc_workspaces() (0) 661da177e4SLinus Torvalds #define free_workspaces() do { } while(0) 671da177e4SLinus Torvalds #endif /* __KERNEL__ */ 681da177e4SLinus Torvalds 6975c96f85SAdrian Bunk static int jffs2_zlib_compress(unsigned char *data_in, 7075c96f85SAdrian Bunk unsigned char *cpage_out, 7175c96f85SAdrian Bunk uint32_t *sourcelen, uint32_t *dstlen, 7275c96f85SAdrian Bunk void *model) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds int ret; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds if (*dstlen <= STREAM_END_SPACE) 771da177e4SLinus Torvalds return -1; 781da177e4SLinus Torvalds 79353ab6e9SIngo Molnar mutex_lock(&deflate_mutex); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds if (Z_OK != zlib_deflateInit(&def_strm, 3)) { 821da177e4SLinus Torvalds printk(KERN_WARNING "deflateInit failed\n"); 83353ab6e9SIngo Molnar mutex_unlock(&deflate_mutex); 841da177e4SLinus Torvalds return -1; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds def_strm.next_in = data_in; 881da177e4SLinus Torvalds def_strm.total_in = 0; 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds def_strm.next_out = cpage_out; 911da177e4SLinus Torvalds def_strm.total_out = 0; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) { 941da177e4SLinus Torvalds def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE); 951da177e4SLinus Torvalds def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out); 961da177e4SLinus Torvalds D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n", 971da177e4SLinus Torvalds def_strm.avail_in, def_strm.avail_out)); 981da177e4SLinus Torvalds ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH); 991da177e4SLinus Torvalds D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n", 1001da177e4SLinus Torvalds def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out)); 1011da177e4SLinus Torvalds if (ret != Z_OK) { 1021da177e4SLinus Torvalds D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret)); 1031da177e4SLinus Torvalds zlib_deflateEnd(&def_strm); 104353ab6e9SIngo Molnar mutex_unlock(&deflate_mutex); 1051da177e4SLinus Torvalds return -1; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds def_strm.avail_out += STREAM_END_SPACE; 1091da177e4SLinus Torvalds def_strm.avail_in = 0; 1101da177e4SLinus Torvalds ret = zlib_deflate(&def_strm, Z_FINISH); 1111da177e4SLinus Torvalds zlib_deflateEnd(&def_strm); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds if (ret != Z_STREAM_END) { 1141da177e4SLinus Torvalds D1(printk(KERN_DEBUG "final deflate returned %d\n", ret)); 1151da177e4SLinus Torvalds ret = -1; 1161da177e4SLinus Torvalds goto out; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds if (def_strm.total_out >= def_strm.total_in) { 1201da177e4SLinus Torvalds D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n", 1211da177e4SLinus Torvalds def_strm.total_in, def_strm.total_out)); 1221da177e4SLinus Torvalds ret = -1; 1231da177e4SLinus Torvalds goto out; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n", 1271da177e4SLinus Torvalds def_strm.total_in, def_strm.total_out)); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds *dstlen = def_strm.total_out; 1301da177e4SLinus Torvalds *sourcelen = def_strm.total_in; 1311da177e4SLinus Torvalds ret = 0; 1321da177e4SLinus Torvalds out: 133353ab6e9SIngo Molnar mutex_unlock(&deflate_mutex); 1341da177e4SLinus Torvalds return ret; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 13775c96f85SAdrian Bunk static int jffs2_zlib_decompress(unsigned char *data_in, 13875c96f85SAdrian Bunk unsigned char *cpage_out, 13975c96f85SAdrian Bunk uint32_t srclen, uint32_t destlen, 14075c96f85SAdrian Bunk void *model) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds int ret; 1431da177e4SLinus Torvalds int wbits = MAX_WBITS; 1441da177e4SLinus Torvalds 145353ab6e9SIngo Molnar mutex_lock(&inflate_mutex); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds inf_strm.next_in = data_in; 1481da177e4SLinus Torvalds inf_strm.avail_in = srclen; 1491da177e4SLinus Torvalds inf_strm.total_in = 0; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds inf_strm.next_out = cpage_out; 1521da177e4SLinus Torvalds inf_strm.avail_out = destlen; 1531da177e4SLinus Torvalds inf_strm.total_out = 0; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* If it's deflate, and it's got no preset dictionary, then 1561da177e4SLinus Torvalds we can tell zlib to skip the adler32 check. */ 1571da177e4SLinus Torvalds if (srclen > 2 && !(data_in[1] & PRESET_DICT) && 1581da177e4SLinus Torvalds ((data_in[0] & 0x0f) == Z_DEFLATED) && 1591da177e4SLinus Torvalds !(((data_in[0]<<8) + data_in[1]) % 31)) { 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds D2(printk(KERN_DEBUG "inflate skipping adler32\n")); 1621da177e4SLinus Torvalds wbits = -((data_in[0] >> 4) + 8); 1631da177e4SLinus Torvalds inf_strm.next_in += 2; 1641da177e4SLinus Torvalds inf_strm.avail_in -= 2; 1651da177e4SLinus Torvalds } else { 1661da177e4SLinus Torvalds /* Let this remain D1 for now -- it should never happen */ 1671da177e4SLinus Torvalds D1(printk(KERN_DEBUG "inflate not skipping adler32\n")); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) { 1721da177e4SLinus Torvalds printk(KERN_WARNING "inflateInit failed\n"); 173353ab6e9SIngo Molnar mutex_unlock(&inflate_mutex); 1741da177e4SLinus Torvalds return 1; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK) 1781da177e4SLinus Torvalds ; 1791da177e4SLinus Torvalds if (ret != Z_STREAM_END) { 1801da177e4SLinus Torvalds printk(KERN_NOTICE "inflate returned %d\n", ret); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds zlib_inflateEnd(&inf_strm); 183353ab6e9SIngo Molnar mutex_unlock(&inflate_mutex); 1841da177e4SLinus Torvalds return 0; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static struct jffs2_compressor jffs2_zlib_comp = { 1881da177e4SLinus Torvalds .priority = JFFS2_ZLIB_PRIORITY, 1891da177e4SLinus Torvalds .name = "zlib", 1901da177e4SLinus Torvalds .compr = JFFS2_COMPR_ZLIB, 1911da177e4SLinus Torvalds .compress = &jffs2_zlib_compress, 1921da177e4SLinus Torvalds .decompress = &jffs2_zlib_decompress, 1931da177e4SLinus Torvalds #ifdef JFFS2_ZLIB_DISABLED 1941da177e4SLinus Torvalds .disabled = 1, 1951da177e4SLinus Torvalds #else 1961da177e4SLinus Torvalds .disabled = 0, 1971da177e4SLinus Torvalds #endif 1981da177e4SLinus Torvalds }; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds int __init jffs2_zlib_init(void) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds int ret; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds ret = alloc_workspaces(); 2051da177e4SLinus Torvalds if (ret) 2061da177e4SLinus Torvalds return ret; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds ret = jffs2_register_compressor(&jffs2_zlib_comp); 2091da177e4SLinus Torvalds if (ret) 2101da177e4SLinus Torvalds free_workspaces(); 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds return ret; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds void jffs2_zlib_exit(void) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds jffs2_unregister_compressor(&jffs2_zlib_comp); 2181da177e4SLinus Torvalds free_workspaces(); 2191da177e4SLinus Torvalds } 220