Path: blob/main/sys/contrib/openzfs/module/zfs/gzip.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2007 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/26272829#include <sys/debug.h>30#include <sys/types.h>31#include <sys/qat.h>32#include <sys/zio_compress.h>3334#ifdef _KERNEL3536#include <sys/zmod.h>37typedef size_t zlen_t;38#define compress_func z_compress_level39#define uncompress_func z_uncompress4041#else /* _KERNEL */4243#include <zlib.h>44typedef uLongf zlen_t;45#define compress_func compress246#define uncompress_func uncompress4748#endif4950static size_t51zfs_gzip_compress_buf(void *s_start, void *d_start, size_t s_len,52size_t d_len, int n)53{54int ret;55zlen_t dstlen = d_len;5657ASSERT(d_len <= s_len);5859/* check if hardware accelerator can be used */60if (qat_dc_use_accel(s_len)) {61ret = qat_compress(QAT_COMPRESS, s_start, s_len, d_start,62d_len, &dstlen);63if (ret == CPA_STATUS_SUCCESS) {64return ((size_t)dstlen);65} else if (ret == CPA_STATUS_INCOMPRESSIBLE) {66if (d_len != s_len)67return (s_len);6869memcpy(d_start, s_start, s_len);70return (s_len);71}72/* if hardware compression fails, do it again with software */73}7475if (compress_func(d_start, &dstlen, s_start, s_len, n) != Z_OK) {76if (d_len != s_len)77return (s_len);7879memcpy(d_start, s_start, s_len);80return (s_len);81}8283return ((size_t)dstlen);84}8586static int87zfs_gzip_decompress_buf(void *s_start, void *d_start, size_t s_len,88size_t d_len, int n)89{90(void) n;91zlen_t dstlen = d_len;9293ASSERT(d_len >= s_len);9495/* check if hardware accelerator can be used */96if (qat_dc_use_accel(d_len)) {97if (qat_compress(QAT_DECOMPRESS, s_start, s_len,98d_start, d_len, &dstlen) == CPA_STATUS_SUCCESS)99return (0);100/* if hardware de-compress fail, do it again with software */101}102103if (uncompress_func(d_start, &dstlen, s_start, s_len) != Z_OK)104return (-1);105106return (0);107}108109ZFS_COMPRESS_WRAP_DECL(zfs_gzip_compress)110ZFS_DECOMPRESS_WRAP_DECL(zfs_gzip_decompress)111112113