xref: /freebsd/sys/contrib/openzfs/module/zfs/lzjb.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * We keep our own copy of this algorithm for 3 main reasons:
28  *	1. If we didn't, anyone modifying common/os/compress.c would
29  *         directly break our on disk format
30  *	2. Our version of lzjb does not have a number of checks that the
31  *         common/os version needs and uses
32  *	3. We initialize the lempel to ensure deterministic results,
33  *	   so that identical blocks can always be deduplicated.
34  * In particular, we are adding the "feature" that compress() can
35  * take a destination buffer size and returns the compressed length, or the
36  * source length if compression would overflow the destination buffer.
37  */
38 
39 #include <sys/zfs_context.h>
40 #include <sys/zio_compress.h>
41 
42 #define	MATCH_BITS	6
43 #define	MATCH_MIN	3
44 #define	MATCH_MAX	((1 << MATCH_BITS) + (MATCH_MIN - 1))
45 #define	OFFSET_MASK	((1 << (16 - MATCH_BITS)) - 1)
46 #define	LEMPEL_SIZE	1024
47 
48 static size_t
49 zfs_lzjb_compress_buf(void *s_start, void *d_start, size_t s_len,
50     size_t d_len, int n)
51 {
52 	(void) n;
53 	uchar_t *src = s_start;
54 	uchar_t *dst = d_start;
55 	uchar_t *cpy;
56 	uchar_t *copymap = NULL;
57 	int copymask = 1 << (NBBY - 1);
58 	int mlen, offset, hash;
59 	uint16_t *hp;
60 	uint16_t *lempel;
61 
62 	lempel = kmem_zalloc(LEMPEL_SIZE * sizeof (uint16_t), KM_SLEEP);
63 	while (src < (uchar_t *)s_start + s_len) {
64 		if ((copymask <<= 1) == (1 << NBBY)) {
65 			if (dst >= (uchar_t *)d_start + d_len - 1 - 2 * NBBY) {
66 				kmem_free(lempel,
67 				    LEMPEL_SIZE*sizeof (uint16_t));
68 				return (s_len);
69 			}
70 			copymask = 1;
71 			copymap = dst;
72 			*dst++ = 0;
73 		}
74 		if (src > (uchar_t *)s_start + s_len - MATCH_MAX) {
75 			*dst++ = *src++;
76 			continue;
77 		}
78 		hash = (src[0] << 16) + (src[1] << 8) + src[2];
79 		hash += hash >> 9;
80 		hash += hash >> 5;
81 		hp = &lempel[hash & (LEMPEL_SIZE - 1)];
82 		offset = (intptr_t)(src - *hp) & OFFSET_MASK;
83 		*hp = (uint16_t)(uintptr_t)src;
84 		cpy = src - offset;
85 		if (cpy >= (uchar_t *)s_start && cpy != src &&
86 		    src[0] == cpy[0] && src[1] == cpy[1] && src[2] == cpy[2]) {
87 			*copymap |= copymask;
88 			for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++)
89 				if (src[mlen] != cpy[mlen])
90 					break;
91 			*dst++ = ((mlen - MATCH_MIN) << (NBBY - MATCH_BITS)) |
92 			    (offset >> NBBY);
93 			*dst++ = (uchar_t)offset;
94 			src += mlen;
95 		} else {
96 			*dst++ = *src++;
97 		}
98 	}
99 
100 	kmem_free(lempel, LEMPEL_SIZE * sizeof (uint16_t));
101 	return (dst - (uchar_t *)d_start);
102 }
103 
104 static int
105 zfs_lzjb_decompress_buf(void *s_start, void *d_start,
106     size_t s_len, size_t d_len, int n)
107 {
108 	(void) s_len, (void) n;
109 	uchar_t *src = s_start;
110 	uchar_t *dst = d_start;
111 	uchar_t *d_end = (uchar_t *)d_start + d_len;
112 	uchar_t *cpy;
113 	uchar_t copymap = 0;
114 	int copymask = 1 << (NBBY - 1);
115 
116 	while (dst < d_end) {
117 		if ((copymask <<= 1) == (1 << NBBY)) {
118 			copymask = 1;
119 			copymap = *src++;
120 		}
121 		if (copymap & copymask) {
122 			int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN;
123 			int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK;
124 			src += 2;
125 			if ((cpy = dst - offset) < (uchar_t *)d_start)
126 				return (-1);
127 			while (--mlen >= 0 && dst < d_end)
128 				*dst++ = *cpy++;
129 		} else {
130 			*dst++ = *src++;
131 		}
132 	}
133 	return (0);
134 }
135 
136 ZFS_COMPRESS_WRAP_DECL(zfs_lzjb_compress)
137 ZFS_DECOMPRESS_WRAP_DECL(zfs_lzjb_decompress)
138