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