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 http://www.opensolaris.org/os/licensing.
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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26 /*
27 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
29 */
30
31 #include <sys/zfs_context.h>
32 #include <sys/compress.h>
33 #include <sys/kstat.h>
34 #include <sys/spa.h>
35 #include <sys/zfeature.h>
36 #include <sys/zio.h>
37 #include <sys/zio_compress.h>
38
39 typedef struct zcomp_stats {
40 kstat_named_t zcompstat_attempts;
41 kstat_named_t zcompstat_empty;
42 kstat_named_t zcompstat_skipped_insufficient_gain;
43 } zcomp_stats_t;
44
45 static zcomp_stats_t zcomp_stats = {
46 { "attempts", KSTAT_DATA_UINT64 },
47 { "empty", KSTAT_DATA_UINT64 },
48 { "skipped_insufficient_gain", KSTAT_DATA_UINT64 }
49 };
50
51 #define ZCOMPSTAT_INCR(stat, val) \
52 atomic_add_64(&zcomp_stats.stat.value.ui64, (val));
53
54 #define ZCOMPSTAT_BUMP(stat) ZCOMPSTAT_INCR(stat, 1);
55
56 kstat_t *zcomp_ksp;
57
58 /*
59 * If nonzero, every 1/X decompression attempts will fail, simulating
60 * an undetected memory error.
61 */
62 uint64_t zio_decompress_fail_fraction = 0;
63
64 /*
65 * Compression vectors.
66 */
67 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
68 {"inherit", 0, NULL, NULL},
69 {"on", 0, NULL, NULL},
70 {"uncompressed", 0, NULL, NULL},
71 {"lzjb", 0, lzjb_compress, lzjb_decompress},
72 {"empty", 0, NULL, NULL},
73 {"gzip-1", 1, gzip_compress, gzip_decompress},
74 {"gzip-2", 2, gzip_compress, gzip_decompress},
75 {"gzip-3", 3, gzip_compress, gzip_decompress},
76 {"gzip-4", 4, gzip_compress, gzip_decompress},
77 {"gzip-5", 5, gzip_compress, gzip_decompress},
78 {"gzip-6", 6, gzip_compress, gzip_decompress},
79 {"gzip-7", 7, gzip_compress, gzip_decompress},
80 {"gzip-8", 8, gzip_compress, gzip_decompress},
81 {"gzip-9", 9, gzip_compress, gzip_decompress},
82 {"zle", 64, zle_compress, zle_decompress},
83 {"lz4", 0, lz4_compress, lz4_decompress}
84 };
85
86 enum zio_compress
zio_compress_select(spa_t * spa,enum zio_compress child,enum zio_compress parent)87 zio_compress_select(spa_t *spa, enum zio_compress child,
88 enum zio_compress parent)
89 {
90 enum zio_compress result;
91
92 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
93 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
94 ASSERT(parent != ZIO_COMPRESS_INHERIT);
95
96 result = child;
97 if (result == ZIO_COMPRESS_INHERIT)
98 result = parent;
99
100 if (result == ZIO_COMPRESS_ON) {
101 if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
102 result = ZIO_COMPRESS_LZ4_ON_VALUE;
103 else
104 result = ZIO_COMPRESS_LEGACY_ON_VALUE;
105 }
106
107 return (result);
108 }
109
110 /*ARGSUSED*/
111 static int
zio_compress_zeroed_cb(void * data,size_t len,void * private)112 zio_compress_zeroed_cb(void *data, size_t len, void *private)
113 {
114 uint64_t *end = (uint64_t *)((char *)data + len);
115 for (uint64_t *word = (uint64_t *)data; word < end; word++)
116 if (*word != 0)
117 return (1);
118
119 return (0);
120 }
121
122 size_t
zio_compress_data(enum zio_compress c,abd_t * src,void * dst,size_t s_len)123 zio_compress_data(enum zio_compress c, abd_t *src, void *dst, size_t s_len)
124 {
125 size_t c_len, d_len;
126 zio_compress_info_t *ci = &zio_compress_table[c];
127
128 ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
129 ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
130
131 ZCOMPSTAT_BUMP(zcompstat_attempts);
132
133 /*
134 * If the data is all zeroes, we don't even need to allocate
135 * a block for it. We indicate this by returning zero size.
136 */
137 if (abd_iterate_func(src, 0, s_len, zio_compress_zeroed_cb, NULL) == 0) {
138 ZCOMPSTAT_BUMP(zcompstat_empty);
139 return (0);
140 }
141
142 if (c == ZIO_COMPRESS_EMPTY)
143 return (s_len);
144
145 /* Compress at least 12.5% */
146 d_len = s_len - (s_len >> 3);
147
148 /* No compression algorithms can read from ABDs directly */
149 void *tmp = abd_borrow_buf_copy(src, s_len);
150 c_len = ci->ci_compress(tmp, dst, s_len, d_len, ci->ci_level);
151 abd_return_buf(src, tmp, s_len);
152
153 if (c_len > d_len) {
154 ZCOMPSTAT_BUMP(zcompstat_skipped_insufficient_gain);
155 return (s_len);
156 }
157
158 ASSERT3U(c_len, <=, d_len);
159 return (c_len);
160 }
161
162 int
zio_decompress_data_buf(enum zio_compress c,void * src,void * dst,size_t s_len,size_t d_len)163 zio_decompress_data_buf(enum zio_compress c, void *src, void *dst,
164 size_t s_len, size_t d_len)
165 {
166 zio_compress_info_t *ci = &zio_compress_table[c];
167 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
168 return (SET_ERROR(EINVAL));
169
170 return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
171 }
172
173 int
zio_decompress_data(enum zio_compress c,abd_t * src,void * dst,size_t s_len,size_t d_len)174 zio_decompress_data(enum zio_compress c, abd_t *src, void *dst,
175 size_t s_len, size_t d_len)
176 {
177 void *tmp = abd_borrow_buf_copy(src, s_len);
178 int ret = zio_decompress_data_buf(c, tmp, dst, s_len, d_len);
179 abd_return_buf(src, tmp, s_len);
180
181 /*
182 * Decompression shouldn't fail, because we've already verifyied
183 * the checksum. However, for extra protection (e.g. against bitflips
184 * in non-ECC RAM), we handle this error (and test it).
185 */
186 ASSERT0(ret);
187 if (zio_decompress_fail_fraction != 0 &&
188 spa_get_random(zio_decompress_fail_fraction) == 0)
189 ret = SET_ERROR(EINVAL);
190
191 return (ret);
192 }
193
194 void
zio_compress_init(void)195 zio_compress_init(void)
196 {
197
198 zcomp_ksp = kstat_create("zfs", 0, "zcompstats", "misc",
199 KSTAT_TYPE_NAMED, sizeof (zcomp_stats) / sizeof (kstat_named_t),
200 KSTAT_FLAG_VIRTUAL);
201
202 if (zcomp_ksp != NULL) {
203 zcomp_ksp->ks_data = &zcomp_stats;
204 kstat_install(zcomp_ksp);
205 }
206 }
207
208 void
zio_compress_fini(void)209 zio_compress_fini(void)
210 {
211 if (zcomp_ksp != NULL) {
212 kstat_delete(zcomp_ksp);
213 zcomp_ksp = NULL;
214 }
215 }
216