xref: /freebsd/sys/contrib/openzfs/module/zfs/gzip.c (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Use is subject to license terms.
25eda14cbcSMatt Macy  */
26eda14cbcSMatt Macy 
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <sys/debug.h>
30eda14cbcSMatt Macy #include <sys/types.h>
31eda14cbcSMatt Macy #include <sys/strings.h>
32eda14cbcSMatt Macy #include <sys/qat.h>
33eda14cbcSMatt Macy #include <sys/zio_compress.h>
34eda14cbcSMatt Macy 
35eda14cbcSMatt Macy #ifdef _KERNEL
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy #include <sys/zmod.h>
38eda14cbcSMatt Macy typedef size_t zlen_t;
39eda14cbcSMatt Macy #define	compress_func	z_compress_level
40eda14cbcSMatt Macy #define	uncompress_func	z_uncompress
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy #else /* _KERNEL */
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy #include <zlib.h>
45eda14cbcSMatt Macy typedef uLongf zlen_t;
46eda14cbcSMatt Macy #define	compress_func	compress2
47eda14cbcSMatt Macy #define	uncompress_func	uncompress
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy #endif
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy size_t
52eda14cbcSMatt Macy gzip_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
53eda14cbcSMatt Macy {
54eda14cbcSMatt Macy 	int ret;
55eda14cbcSMatt Macy 	zlen_t dstlen = d_len;
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy 	ASSERT(d_len <= s_len);
58eda14cbcSMatt Macy 
59eda14cbcSMatt Macy 	/* check if hardware accelerator can be used */
60eda14cbcSMatt Macy 	if (qat_dc_use_accel(s_len)) {
61eda14cbcSMatt Macy 		ret = qat_compress(QAT_COMPRESS, s_start, s_len, d_start,
62eda14cbcSMatt Macy 		    d_len, &dstlen);
63eda14cbcSMatt Macy 		if (ret == CPA_STATUS_SUCCESS) {
64eda14cbcSMatt Macy 			return ((size_t)dstlen);
65eda14cbcSMatt Macy 		} else if (ret == CPA_STATUS_INCOMPRESSIBLE) {
66eda14cbcSMatt Macy 			if (d_len != s_len)
67eda14cbcSMatt Macy 				return (s_len);
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy 			bcopy(s_start, d_start, s_len);
70eda14cbcSMatt Macy 			return (s_len);
71eda14cbcSMatt Macy 		}
72eda14cbcSMatt Macy 		/* if hardware compression fails, do it again with software */
73eda14cbcSMatt Macy 	}
74eda14cbcSMatt Macy 
75eda14cbcSMatt Macy 	if (compress_func(d_start, &dstlen, s_start, s_len, n) != Z_OK) {
76eda14cbcSMatt Macy 		if (d_len != s_len)
77eda14cbcSMatt Macy 			return (s_len);
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy 		bcopy(s_start, d_start, s_len);
80eda14cbcSMatt Macy 		return (s_len);
81eda14cbcSMatt Macy 	}
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	return ((size_t)dstlen);
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy int
87eda14cbcSMatt Macy gzip_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
88eda14cbcSMatt Macy {
89*e92ffd9bSMartin Matuska 	(void) n;
90eda14cbcSMatt Macy 	zlen_t dstlen = d_len;
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy 	ASSERT(d_len >= s_len);
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	/* check if hardware accelerator can be used */
95eda14cbcSMatt Macy 	if (qat_dc_use_accel(d_len)) {
96eda14cbcSMatt Macy 		if (qat_compress(QAT_DECOMPRESS, s_start, s_len,
97eda14cbcSMatt Macy 		    d_start, d_len, &dstlen) == CPA_STATUS_SUCCESS)
98eda14cbcSMatt Macy 			return (0);
99eda14cbcSMatt Macy 		/* if hardware de-compress fail, do it again with software */
100eda14cbcSMatt Macy 	}
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy 	if (uncompress_func(d_start, &dstlen, s_start, s_len) != Z_OK)
103eda14cbcSMatt Macy 		return (-1);
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy 	return (0);
106eda14cbcSMatt Macy }
107