xref: /freebsd/sys/contrib/openzfs/module/zfs/gzip.c (revision da5137abdf463bb5fee85061958a14dd12bc043e)
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/qat.h>
32eda14cbcSMatt Macy #include <sys/zio_compress.h>
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy #ifdef _KERNEL
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy #include <sys/zmod.h>
37eda14cbcSMatt Macy typedef size_t zlen_t;
38eda14cbcSMatt Macy #define	compress_func	z_compress_level
39eda14cbcSMatt Macy #define	uncompress_func	z_uncompress
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy #else /* _KERNEL */
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy #include <zlib.h>
44eda14cbcSMatt Macy typedef uLongf zlen_t;
45eda14cbcSMatt Macy #define	compress_func	compress2
46eda14cbcSMatt Macy #define	uncompress_func	uncompress
47eda14cbcSMatt Macy 
48eda14cbcSMatt Macy #endif
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy size_t
51eda14cbcSMatt Macy gzip_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
52eda14cbcSMatt Macy {
53eda14cbcSMatt Macy 	int ret;
54eda14cbcSMatt Macy 	zlen_t dstlen = d_len;
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy 	ASSERT(d_len <= s_len);
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy 	/* check if hardware accelerator can be used */
59eda14cbcSMatt Macy 	if (qat_dc_use_accel(s_len)) {
60eda14cbcSMatt Macy 		ret = qat_compress(QAT_COMPRESS, s_start, s_len, d_start,
61eda14cbcSMatt Macy 		    d_len, &dstlen);
62eda14cbcSMatt Macy 		if (ret == CPA_STATUS_SUCCESS) {
63eda14cbcSMatt Macy 			return ((size_t)dstlen);
64eda14cbcSMatt Macy 		} else if (ret == CPA_STATUS_INCOMPRESSIBLE) {
65eda14cbcSMatt Macy 			if (d_len != s_len)
66eda14cbcSMatt Macy 				return (s_len);
67eda14cbcSMatt Macy 
68*da5137abSMartin Matuska 			memcpy(d_start, s_start, s_len);
69eda14cbcSMatt Macy 			return (s_len);
70eda14cbcSMatt Macy 		}
71eda14cbcSMatt Macy 		/* if hardware compression fails, do it again with software */
72eda14cbcSMatt Macy 	}
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy 	if (compress_func(d_start, &dstlen, s_start, s_len, n) != Z_OK) {
75eda14cbcSMatt Macy 		if (d_len != s_len)
76eda14cbcSMatt Macy 			return (s_len);
77eda14cbcSMatt Macy 
78*da5137abSMartin Matuska 		memcpy(d_start, s_start, s_len);
79eda14cbcSMatt Macy 		return (s_len);
80eda14cbcSMatt Macy 	}
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy 	return ((size_t)dstlen);
83eda14cbcSMatt Macy }
84eda14cbcSMatt Macy 
85eda14cbcSMatt Macy int
86eda14cbcSMatt Macy gzip_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
87eda14cbcSMatt Macy {
88e92ffd9bSMartin Matuska 	(void) n;
89eda14cbcSMatt Macy 	zlen_t dstlen = d_len;
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 	ASSERT(d_len >= s_len);
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 	/* check if hardware accelerator can be used */
94eda14cbcSMatt Macy 	if (qat_dc_use_accel(d_len)) {
95eda14cbcSMatt Macy 		if (qat_compress(QAT_DECOMPRESS, s_start, s_len,
96eda14cbcSMatt Macy 		    d_start, d_len, &dstlen) == CPA_STATUS_SUCCESS)
97eda14cbcSMatt Macy 			return (0);
98eda14cbcSMatt Macy 		/* if hardware de-compress fail, do it again with software */
99eda14cbcSMatt Macy 	}
100eda14cbcSMatt Macy 
101eda14cbcSMatt Macy 	if (uncompress_func(d_start, &dstlen, s_start, s_len) != Z_OK)
102eda14cbcSMatt Macy 		return (-1);
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 	return (0);
105eda14cbcSMatt Macy }
106