2 * fs/logfs/compr.c - compression routines
4 * As should be obvious for Linux kernel code, license is GPLv2
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
9 #include <linux/vmalloc.h>
10 #include <linux/zlib.h>
14 static DEFINE_MUTEX(compr_mutex);
15 static struct z_stream_s stream;
17 int logfs_compress(void *in, void *out, size_t inlen, size_t outlen)
22 mutex_lock(&compr_mutex);
23 err = zlib_deflateInit(&stream, COMPR_LEVEL);
28 stream.avail_in = inlen;
30 stream.next_out = out;
31 stream.avail_out = outlen;
34 err = zlib_deflate(&stream, Z_FINISH);
35 if (err != Z_STREAM_END)
38 err = zlib_deflateEnd(&stream);
42 if (stream.total_out >= stream.total_in)
45 ret = stream.total_out;
47 mutex_unlock(&compr_mutex);
51 int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen)
56 mutex_lock(&compr_mutex);
57 err = zlib_inflateInit(&stream);
62 stream.avail_in = inlen;
64 stream.next_out = out;
65 stream.avail_out = outlen;
68 err = zlib_inflate(&stream, Z_FINISH);
69 if (err != Z_STREAM_END)
72 err = zlib_inflateEnd(&stream);
78 mutex_unlock(&compr_mutex);
82 int __init logfs_compr_init(void)
84 size_t size = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
85 zlib_inflate_workspacesize());
86 stream.workspace = vmalloc(size);
87 if (!stream.workspace)
92 void logfs_compr_exit(void)
94 vfree(stream.workspace);