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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25 */ 26 27 /* 28 * We keep our own copy of this algorithm for 3 main reasons: 29 * 1. If we didn't, anyone modifying common/os/compress.c would 30 * directly break our on disk format 31 * 2. Our version of lzjb does not have a number of checks that the 32 * common/os version needs and uses 33 * 3. We initialize the lempel to ensure deterministic results, 34 * so that identical blocks can always be deduplicated. 35 * In particular, we are adding the "feature" that compress() can 36 * take a destination buffer size and returns the compressed length, or the 37 * source length if compression would overflow the destination buffer. 38 */ 39 40 #include <sys/zfs_context.h> 41 #include <sys/zio_compress.h> 42 43 #define MATCH_BITS 6 44 #define MATCH_MIN 3 45 #define MATCH_MAX ((1 << MATCH_BITS) + (MATCH_MIN - 1)) 46 #define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1) 47 #define LEMPEL_SIZE 1024 48 49 static size_t 50 zfs_lzjb_compress_buf(void *s_start, void *d_start, size_t s_len, 51 size_t d_len, int n) 52 { 53 (void) n; 54 uchar_t *src = s_start; 55 uchar_t *dst = d_start; 56 uchar_t *cpy; 57 uchar_t *copymap = NULL; 58 int copymask = 1 << (NBBY - 1); 59 int mlen, offset, hash; 60 uint16_t *hp; 61 uint16_t *lempel; 62 63 lempel = kmem_zalloc(LEMPEL_SIZE * sizeof (uint16_t), KM_SLEEP); 64 while (src < (uchar_t *)s_start + s_len) { 65 if ((copymask <<= 1) == (1 << NBBY)) { 66 if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) { 67 kmem_free(lempel, 68 LEMPEL_SIZE*sizeof (uint16_t)); 69 return (s_len); 70 } 71 copymask = 1; 72 copymap = dst; 73 *dst++ = 0; 74 } 75 if (src > (uchar_t *)s_start + s_len - MATCH_MAX) { 76 *dst++ = *src++; 77 continue; 78 } 79 hash = (src[0] << 16) + (src[1] << 8) + src[2]; 80 hash += hash >> 9; 81 hash += hash >> 5; 82 hp = &lempel[hash & (LEMPEL_SIZE - 1)]; 83 offset = (intptr_t)(src - *hp) & OFFSET_MASK; 84 *hp = (uint16_t)(uintptr_t)src; 85 cpy = src - offset; 86 if (cpy >= (uchar_t *)s_start && cpy != src && 87 src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) { 88 *copymap |= copymask; 89 for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++) 90 if (src[mlen] != cpy[mlen]) 91 break; 92 *dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) | 93 (offset >> NBBY); 94 *dst++ = (uchar_t)offset; 95 src += mlen; 96 } else { 97 *dst++ = *src++; 98 } 99 } 100 101 kmem_free(lempel, LEMPEL_SIZE * sizeof (uint16_t)); 102 return (dst - (uchar_t *)d_start); 103 } 104 105 static int 106 zfs_lzjb_decompress_buf(void *s_start, void *d_start, 107 size_t s_len, size_t d_len, int n) 108 { 109 (void) s_len, (void) n; 110 uchar_t *src = s_start; 111 uchar_t *dst = d_start; 112 uchar_t *d_end = (uchar_t *)d_start + d_len; 113 uchar_t *cpy; 114 uchar_t copymap = 0; 115 int copymask = 1 << (NBBY - 1); 116 117 while (dst < d_end) { 118 if ((copymask <<= 1) == (1 << NBBY)) { 119 copymask = 1; 120 copymap = *src++; 121 } 122 if (copymap & copymask) { 123 int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN; 124 int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK; 125 src += 2; 126 if ((cpy = dst - offset) < (uchar_t *)d_start) 127 return (-1); 128 while (--mlen >= 0 && dst < d_end) 129 *dst++ = *cpy++; 130 } else { 131 *dst++ = *src++; 132 } 133 } 134 return (0); 135 } 136 137 ZFS_COMPRESS_WRAP_DECL(zfs_lzjb_compress) 138 ZFS_DECOMPRESS_WRAP_DECL(zfs_lzjb_decompress) 139