xref: /freebsd-src/sys/contrib/openzfs/module/zfs/gzip.c (revision e2df9bb44109577475aeb186e7186ac040f9bde1)
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
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
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 
50*e2df9bb4SMartin Matuska static size_t
51*e2df9bb4SMartin Matuska zfs_gzip_compress_buf(void *s_start, void *d_start, size_t s_len,
52*e2df9bb4SMartin Matuska     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 
69da5137abSMartin Matuska 			memcpy(d_start, s_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 
79da5137abSMartin Matuska 		memcpy(d_start, s_start, s_len);
80eda14cbcSMatt Macy 		return (s_len);
81eda14cbcSMatt Macy 	}
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	return ((size_t)dstlen);
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy 
86*e2df9bb4SMartin Matuska static int
87*e2df9bb4SMartin Matuska zfs_gzip_decompress_buf(void *s_start, void *d_start, size_t s_len,
88*e2df9bb4SMartin Matuska     size_t d_len, int n)
89eda14cbcSMatt Macy {
90e92ffd9bSMartin Matuska 	(void) n;
91eda14cbcSMatt Macy 	zlen_t dstlen = d_len;
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 	ASSERT(d_len >= s_len);
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy 	/* check if hardware accelerator can be used */
96eda14cbcSMatt Macy 	if (qat_dc_use_accel(d_len)) {
97eda14cbcSMatt Macy 		if (qat_compress(QAT_DECOMPRESS, s_start, s_len,
98eda14cbcSMatt Macy 		    d_start, d_len, &dstlen) == CPA_STATUS_SUCCESS)
99eda14cbcSMatt Macy 			return (0);
100eda14cbcSMatt Macy 		/* if hardware de-compress fail, do it again with software */
101eda14cbcSMatt Macy 	}
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy 	if (uncompress_func(d_start, &dstlen, s_start, s_len) != Z_OK)
104eda14cbcSMatt Macy 		return (-1);
105eda14cbcSMatt Macy 
106eda14cbcSMatt Macy 	return (0);
107eda14cbcSMatt Macy }
108*e2df9bb4SMartin Matuska 
109*e2df9bb4SMartin Matuska ZFS_COMPRESS_WRAP_DECL(zfs_gzip_compress)
110*e2df9bb4SMartin Matuska ZFS_DECOMPRESS_WRAP_DECL(zfs_gzip_decompress)
111