]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zio_compress.c
MFV r318946: 8021 ARC buf data scatter-ization
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zio_compress.c
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, 2016 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  * Compression vectors.
60  */
61 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
62         {"inherit",             0,      NULL,           NULL},
63         {"on",                  0,      NULL,           NULL},
64         {"uncompressed",        0,      NULL,           NULL},
65         {"lzjb",                0,      lzjb_compress,  lzjb_decompress},
66         {"empty",               0,      NULL,           NULL},
67         {"gzip-1",              1,      gzip_compress,  gzip_decompress},
68         {"gzip-2",              2,      gzip_compress,  gzip_decompress},
69         {"gzip-3",              3,      gzip_compress,  gzip_decompress},
70         {"gzip-4",              4,      gzip_compress,  gzip_decompress},
71         {"gzip-5",              5,      gzip_compress,  gzip_decompress},
72         {"gzip-6",              6,      gzip_compress,  gzip_decompress},
73         {"gzip-7",              7,      gzip_compress,  gzip_decompress},
74         {"gzip-8",              8,      gzip_compress,  gzip_decompress},
75         {"gzip-9",              9,      gzip_compress,  gzip_decompress},
76         {"zle",                 64,     zle_compress,   zle_decompress},
77         {"lz4",                 0,      lz4_compress,   lz4_decompress}
78 };
79
80 enum zio_compress
81 zio_compress_select(spa_t *spa, enum zio_compress child,
82     enum zio_compress parent)
83 {
84         enum zio_compress result;
85
86         ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
87         ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
88         ASSERT(parent != ZIO_COMPRESS_INHERIT);
89
90         result = child;
91         if (result == ZIO_COMPRESS_INHERIT)
92                 result = parent;
93
94         if (result == ZIO_COMPRESS_ON) {
95                 if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
96                         result = ZIO_COMPRESS_LZ4_ON_VALUE;
97                 else
98                         result = ZIO_COMPRESS_LEGACY_ON_VALUE;
99         }
100
101         return (result);
102 }
103
104 /*ARGSUSED*/
105 static int
106 zio_compress_zeroed_cb(void *data, size_t len, void *private)
107 {
108         uint64_t *end = (uint64_t *)((char *)data + len);
109         for (uint64_t *word = (uint64_t *)data; word < end; word++)
110                 if (*word != 0)
111                         return (1);
112
113         return (0);
114 }
115
116 size_t
117 zio_compress_data(enum zio_compress c, abd_t *src, void *dst, size_t s_len)
118 {
119         size_t c_len, d_len;
120         zio_compress_info_t *ci = &zio_compress_table[c];
121
122         ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
123         ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
124
125         ZCOMPSTAT_BUMP(zcompstat_attempts);
126
127         /*
128          * If the data is all zeroes, we don't even need to allocate
129          * a block for it.  We indicate this by returning zero size.
130          */
131         if (abd_iterate_func(src, 0, s_len, zio_compress_zeroed_cb, NULL) == 0) {
132                 ZCOMPSTAT_BUMP(zcompstat_empty);
133                 return (0);
134         }
135
136         if (c == ZIO_COMPRESS_EMPTY)
137                 return (s_len);
138
139         /* Compress at least 12.5% */
140         d_len = s_len - (s_len >> 3);
141
142         /* No compression algorithms can read from ABDs directly */
143         void *tmp = abd_borrow_buf_copy(src, s_len);
144         c_len = ci->ci_compress(tmp, dst, s_len, d_len, ci->ci_level);
145         abd_return_buf(src, tmp, s_len);
146
147         if (c_len > d_len) {
148                 ZCOMPSTAT_BUMP(zcompstat_skipped_insufficient_gain);
149                 return (s_len);
150         }
151
152         ASSERT3U(c_len, <=, d_len);
153         return (c_len);
154 }
155
156 int
157 zio_decompress_data_buf(enum zio_compress c, void *src, void *dst,
158     size_t s_len, size_t d_len)
159 {
160         zio_compress_info_t *ci = &zio_compress_table[c];
161         if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
162                 return (SET_ERROR(EINVAL));
163
164         return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
165 }
166
167 int
168 zio_decompress_data(enum zio_compress c, abd_t *src, void *dst,
169     size_t s_len, size_t d_len)
170 {
171         void *tmp = abd_borrow_buf_copy(src, s_len);
172         int ret = zio_decompress_data_buf(c, tmp, dst, s_len, d_len);
173         abd_return_buf(src, tmp, s_len);
174
175         return (ret);
176 }
177
178 void
179 zio_compress_init(void)
180 {
181
182         zcomp_ksp = kstat_create("zfs", 0, "zcompstats", "misc",
183             KSTAT_TYPE_NAMED, sizeof (zcomp_stats) / sizeof (kstat_named_t),
184             KSTAT_FLAG_VIRTUAL);
185
186         if (zcomp_ksp != NULL) {
187                 zcomp_ksp->ks_data = &zcomp_stats;
188                 kstat_install(zcomp_ksp);
189         }
190 }
191
192 void
193 zio_compress_fini(void)
194 {
195         if (zcomp_ksp != NULL) {
196                 kstat_delete(zcomp_ksp);
197                 zcomp_ksp = NULL;
198         }
199 }