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