mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2024-12-24 02:29:32 +03:00
Fix zlib compression
While portions of the code needed to support z_compress_level() and z_uncompress() where in place. In reality the current implementation was non-functional, it just was compilable. The critical missing component was to setup a workspace for the compress/uncompress stream structures to use. A kmem_cache was added for the workspace area because we require a large chunk of memory. This avoids to need to continually alloc/free this memory and vmap() the pages which is very slow. Several objects will reside in the per-cpu kmem_cache making them quick to acquire and release. A further optimization would be to adjust the implementation to additional ensure the memory is local to the cpu. Currently that may not be the case.
This commit is contained in:
parent
5a52a782a0
commit
5c1967ebe2
@ -64,6 +64,7 @@
|
||||
#define SS_KSTAT 0x00020000
|
||||
#define SS_XDR 0x00040000
|
||||
#define SS_TSD 0x00080000
|
||||
#define SS_ZLIB 0x00100000
|
||||
#define SS_USER1 0x01000000
|
||||
#define SS_USER2 0x02000000
|
||||
#define SS_USER3 0x04000000
|
||||
|
@ -55,105 +55,15 @@
|
||||
#ifndef _SPL_ZMOD_H
|
||||
#define _SPL_ZMOD_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <linux/zlib.h>
|
||||
|
||||
/*
|
||||
* Compresses the source buffer into the destination buffer. The level
|
||||
* parameter has the same meaning as in deflateInit. sourceLen is the byte
|
||||
* length of the source buffer. Upon entry, destLen is the total size of the
|
||||
* destination buffer, which must be at least 0.1% larger than sourceLen plus
|
||||
* 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
|
||||
*
|
||||
* compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
|
||||
* memory, Z_BUF_ERROR if there was not enough room in the output buffer,
|
||||
* Z_STREAM_ERROR if the level parameter is invalid.
|
||||
*/
|
||||
static __inline__ int
|
||||
z_compress_level(void *dest, size_t *destLen, const void *source,
|
||||
size_t sourceLen, int level)
|
||||
{
|
||||
z_stream stream;
|
||||
int err;
|
||||
extern int z_compress_level(void *dest, size_t *destLen, const void *source,
|
||||
size_t sourceLen, int level);
|
||||
extern int z_uncompress(void *dest, size_t *destLen, const void *source,
|
||||
size_t sourceLen);
|
||||
|
||||
stream.next_in = (Byte *)source;
|
||||
stream.avail_in = (uInt)sourceLen;
|
||||
#ifdef MAXSEG_64K
|
||||
/* Check for source > 64K on 16-bit machine: */
|
||||
if ((size_t)stream.avail_in != sourceLen)
|
||||
return Z_BUF_ERROR;
|
||||
#endif
|
||||
stream.next_out = dest;
|
||||
stream.avail_out = (uInt)*destLen;
|
||||
|
||||
if ((size_t)stream.avail_out != *destLen)
|
||||
return Z_BUF_ERROR;
|
||||
|
||||
err = zlib_deflateInit(&stream, level);
|
||||
if (err != Z_OK)
|
||||
return err;
|
||||
|
||||
err = zlib_deflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
zlib_deflateEnd(&stream);
|
||||
return err == Z_OK ? Z_BUF_ERROR : err;
|
||||
}
|
||||
*destLen = stream.total_out;
|
||||
|
||||
err = zlib_deflateEnd(&stream);
|
||||
return err;
|
||||
} /* z_compress_level() */
|
||||
|
||||
/*
|
||||
* Decompresses the source buffer into the destination buffer. sourceLen is
|
||||
* the byte length of the source buffer. Upon entry, destLen is the total
|
||||
* size of the destination buffer, which must be large enough to hold the
|
||||
* entire uncompressed data. (The size of the uncompressed data must have
|
||||
* been saved previously by the compressor and transmitted to the decompressor
|
||||
* by some mechanism outside the scope of this compression library.)
|
||||
* Upon exit, destLen is the actual size of the compressed buffer.
|
||||
* This function can be used to decompress a whole file at once if the
|
||||
* input file is mmap'ed.
|
||||
*
|
||||
* uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
|
||||
* enough memory, Z_BUF_ERROR if there was not enough room in the output
|
||||
* buffer, or Z_DATA_ERROR if the input data was corrupted.
|
||||
*/
|
||||
static __inline__ int
|
||||
z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
|
||||
{
|
||||
z_stream stream;
|
||||
int err;
|
||||
|
||||
stream.next_in = (Byte *)source;
|
||||
stream.avail_in = (uInt)sourceLen;
|
||||
/* Check for source > 64K on 16-bit machine: */
|
||||
if ((size_t)stream.avail_in != sourceLen)
|
||||
return Z_BUF_ERROR;
|
||||
|
||||
stream.next_out = dest;
|
||||
stream.avail_out = (uInt)*destLen;
|
||||
|
||||
if ((size_t)stream.avail_out != *destLen)
|
||||
return Z_BUF_ERROR;
|
||||
|
||||
err = zlib_inflateInit(&stream);
|
||||
if (err != Z_OK)
|
||||
return err;
|
||||
|
||||
err = zlib_inflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
zlib_inflateEnd(&stream);
|
||||
|
||||
if (err == Z_NEED_DICT ||
|
||||
(err == Z_BUF_ERROR && stream.avail_in == 0))
|
||||
return Z_DATA_ERROR;
|
||||
|
||||
return err;
|
||||
}
|
||||
*destLen = stream.total_out;
|
||||
|
||||
err = zlib_inflateEnd(&stream);
|
||||
return err;
|
||||
} /* z_uncompress() */
|
||||
int zlib_init(void);
|
||||
void zlib_fini(void);
|
||||
|
||||
#endif /* SPL_ZMOD_H */
|
||||
|
@ -27,3 +27,4 @@ spl-objs += @top_srcdir@/module/spl/spl-condvar.o
|
||||
spl-objs += @top_srcdir@/module/spl/spl-xdr.o
|
||||
spl-objs += @top_srcdir@/module/spl/spl-cred.o
|
||||
spl-objs += @top_srcdir@/module/spl/spl-tsd.o
|
||||
spl-objs += @top_srcdir@/module/spl/spl-zlib.o
|
||||
|
@ -160,6 +160,8 @@ spl_debug_subsys2str(int subsys)
|
||||
return "xdr";
|
||||
case SS_TSD:
|
||||
return "tsd";
|
||||
case SS_ZLIB:
|
||||
return "zlib";
|
||||
case SS_USER1:
|
||||
return "user1";
|
||||
case SS_USER2:
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include <sys/rwlock.h>
|
||||
#include <sys/taskq.h>
|
||||
#include <sys/tsd.h>
|
||||
#include <sys/zmod.h>
|
||||
#include <sys/debug.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/kstat.h>
|
||||
@ -471,20 +472,25 @@ __init spl_init(void)
|
||||
if ((rc = tsd_init()))
|
||||
SGOTO(out8, rc);
|
||||
|
||||
if ((rc = zlib_init()))
|
||||
SGOTO(out9, rc);
|
||||
|
||||
if ((rc = set_hostid()))
|
||||
SGOTO(out9, rc = -EADDRNOTAVAIL);
|
||||
SGOTO(out10, rc = -EADDRNOTAVAIL);
|
||||
|
||||
#ifndef HAVE_KALLSYMS_LOOKUP_NAME
|
||||
if ((rc = set_kallsyms_lookup_name()))
|
||||
SGOTO(out9, rc = -EADDRNOTAVAIL);
|
||||
SGOTO(out10, rc = -EADDRNOTAVAIL);
|
||||
#endif /* HAVE_KALLSYMS_LOOKUP_NAME */
|
||||
|
||||
if ((rc = spl_kmem_init_kallsyms_lookup()))
|
||||
SGOTO(out9, rc);
|
||||
SGOTO(out10, rc);
|
||||
|
||||
printk(KERN_NOTICE "SPL: Loaded Solaris Porting Layer v%s%s\n",
|
||||
SPL_META_VERSION, SPL_DEBUG_STR);
|
||||
SRETURN(rc);
|
||||
out10:
|
||||
zlib_fini();
|
||||
out9:
|
||||
tsd_fini();
|
||||
out8:
|
||||
@ -516,6 +522,7 @@ spl_fini(void)
|
||||
|
||||
printk(KERN_NOTICE "SPL: Unloaded Solaris Porting Layer v%s%s\n",
|
||||
SPL_META_VERSION, SPL_DEBUG_STR);
|
||||
zlib_fini();
|
||||
tsd_fini();
|
||||
kstat_fini();
|
||||
proc_fini();
|
||||
|
217
module/spl/spl-zlib.c
Normal file
217
module/spl/spl-zlib.c
Normal file
@ -0,0 +1,217 @@
|
||||
/*****************************************************************************\
|
||||
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
||||
* Copyright (C) 2007 The Regents of the University of California.
|
||||
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
||||
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
||||
* UCRL-CODE-235197
|
||||
*
|
||||
* This file is part of the SPL, Solaris Porting Layer.
|
||||
* For details, see <http://github.com/behlendorf/spl/>.
|
||||
*
|
||||
* The SPL is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************
|
||||
* z_compress_level/z_uncompress are nearly identical copies of the
|
||||
* compress2/uncompress functions provided by the official zlib package
|
||||
* available at http://zlib.net/. The only changes made we to slightly
|
||||
* adapt the functions called to match the linux kernel implementation
|
||||
* of zlib. The full zlib license follows:
|
||||
*
|
||||
* zlib.h -- interface of the 'zlib' general purpose compression library
|
||||
* version 1.2.5, April 19th, 2010
|
||||
*
|
||||
* Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
* Jean-loup Gailly
|
||||
* Mark Adler
|
||||
\*****************************************************************************/
|
||||
|
||||
|
||||
#include <sys/kmem.h>
|
||||
#include <sys/zmod.h>
|
||||
#include <spl-debug.h>
|
||||
|
||||
#ifdef DEBUG_SUBSYSTEM
|
||||
#undef DEBUG_SUBSYSTEM
|
||||
#endif
|
||||
|
||||
#define DEBUG_SUBSYSTEM SS_ZLIB
|
||||
|
||||
static spl_kmem_cache_t *zlib_workspace_cache;
|
||||
|
||||
/*
|
||||
* A kmem_cache is used for the zlib workspaces to avoid having to vmalloc
|
||||
* and vfree for every call. Using a kmem_cache also has the advantage
|
||||
* that improves the odds that the memory used will be local to this cpu.
|
||||
* To further improve things it might be wise to create a dedicated per-cpu
|
||||
* workspace for use. This would take some additional care because we then
|
||||
* must disable preemption around the critical section, and verify that
|
||||
* zlib_deflate* and zlib_inflate* never internally call schedule().
|
||||
*/
|
||||
static void *
|
||||
zlib_workspace_alloc(int flags)
|
||||
{
|
||||
return kmem_cache_alloc(zlib_workspace_cache, flags & ~(__GFP_FS));
|
||||
}
|
||||
|
||||
static void
|
||||
zlib_workspace_free(void *workspace)
|
||||
{
|
||||
kmem_cache_free(zlib_workspace_cache, workspace);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compresses the source buffer into the destination buffer. The level
|
||||
* parameter has the same meaning as in deflateInit. sourceLen is the byte
|
||||
* length of the source buffer. Upon entry, destLen is the total size of the
|
||||
* destination buffer, which must be at least 0.1% larger than sourceLen plus
|
||||
* 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
|
||||
*
|
||||
* compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
|
||||
* memory, Z_BUF_ERROR if there was not enough room in the output buffer,
|
||||
* Z_STREAM_ERROR if the level parameter is invalid.
|
||||
*/
|
||||
int
|
||||
z_compress_level(void *dest, size_t *destLen, const void *source,
|
||||
size_t sourceLen, int level)
|
||||
{
|
||||
z_stream stream;
|
||||
int err;
|
||||
|
||||
stream.next_in = (Byte *)source;
|
||||
stream.avail_in = (uInt)sourceLen;
|
||||
stream.next_out = dest;
|
||||
stream.avail_out = (uInt)*destLen;
|
||||
|
||||
if ((size_t)stream.avail_out != *destLen)
|
||||
return Z_BUF_ERROR;
|
||||
|
||||
stream.workspace = zlib_workspace_alloc(KM_SLEEP);
|
||||
if (!stream.workspace)
|
||||
return Z_MEM_ERROR;
|
||||
|
||||
err = zlib_deflateInit(&stream, level);
|
||||
if (err != Z_OK) {
|
||||
zlib_workspace_free(stream.workspace);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = zlib_deflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
zlib_deflateEnd(&stream);
|
||||
zlib_workspace_free(stream.workspace);
|
||||
return err == Z_OK ? Z_BUF_ERROR : err;
|
||||
}
|
||||
*destLen = stream.total_out;
|
||||
|
||||
err = zlib_deflateEnd(&stream);
|
||||
zlib_workspace_free(stream.workspace);
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL(z_compress_level);
|
||||
|
||||
/*
|
||||
* Decompresses the source buffer into the destination buffer. sourceLen is
|
||||
* the byte length of the source buffer. Upon entry, destLen is the total
|
||||
* size of the destination buffer, which must be large enough to hold the
|
||||
* entire uncompressed data. (The size of the uncompressed data must have
|
||||
* been saved previously by the compressor and transmitted to the decompressor
|
||||
* by some mechanism outside the scope of this compression library.)
|
||||
* Upon exit, destLen is the actual size of the compressed buffer.
|
||||
* This function can be used to decompress a whole file at once if the
|
||||
* input file is mmap'ed.
|
||||
*
|
||||
* uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
|
||||
* enough memory, Z_BUF_ERROR if there was not enough room in the output
|
||||
* buffer, or Z_DATA_ERROR if the input data was corrupted.
|
||||
*/
|
||||
int
|
||||
z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
|
||||
{
|
||||
z_stream stream;
|
||||
int err;
|
||||
|
||||
stream.next_in = (Byte *)source;
|
||||
stream.avail_in = (uInt)sourceLen;
|
||||
stream.next_out = dest;
|
||||
stream.avail_out = (uInt)*destLen;
|
||||
|
||||
if ((size_t)stream.avail_out != *destLen)
|
||||
return Z_BUF_ERROR;
|
||||
|
||||
stream.workspace = zlib_workspace_alloc(KM_SLEEP);
|
||||
if (!stream.workspace)
|
||||
return Z_MEM_ERROR;
|
||||
|
||||
err = zlib_inflateInit(&stream);
|
||||
if (err != Z_OK) {
|
||||
zlib_workspace_free(stream.workspace);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = zlib_inflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
zlib_inflateEnd(&stream);
|
||||
zlib_workspace_free(stream.workspace);
|
||||
|
||||
if (err == Z_NEED_DICT ||
|
||||
(err == Z_BUF_ERROR && stream.avail_in == 0))
|
||||
return Z_DATA_ERROR;
|
||||
|
||||
return err;
|
||||
}
|
||||
*destLen = stream.total_out;
|
||||
|
||||
err = zlib_inflateEnd(&stream);
|
||||
zlib_workspace_free(stream.workspace);
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL(z_uncompress);
|
||||
|
||||
int zlib_init(void)
|
||||
{
|
||||
SENTRY;
|
||||
zlib_workspace_cache = kmem_cache_create("spl_zlib_workspace_cache",
|
||||
max(zlib_deflate_workspacesize(), zlib_inflate_workspacesize()),
|
||||
0, NULL, NULL, NULL, NULL, NULL, KMC_VMEM);
|
||||
if (!zlib_workspace_cache)
|
||||
SRETURN(1);
|
||||
|
||||
SRETURN(0);
|
||||
}
|
||||
|
||||
void zlib_fini(void)
|
||||
{
|
||||
SENTRY;
|
||||
kmem_cache_destroy(zlib_workspace_cache);
|
||||
zlib_workspace_cache = NULL;
|
||||
SEXIT;
|
||||
}
|
Loading…
Reference in New Issue
Block a user