]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zio_compress.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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  */
29
30 /*
31  * Copyright (c) 2013 by Delphix. All rights reserved.
32  */
33
34 #include <sys/zfs_context.h>
35 #include <sys/compress.h>
36 #include <sys/kstat.h>
37 #include <sys/spa.h>
38 #include <sys/zio.h>
39 #include <sys/zio_compress.h>
40
41 typedef struct zcomp_stats {
42         kstat_named_t zcompstat_attempts;
43         kstat_named_t zcompstat_empty;
44         kstat_named_t zcompstat_skipped_minblocksize;
45         kstat_named_t zcompstat_skipped_insufficient_gain;
46 } zcomp_stats_t;
47
48 static zcomp_stats_t zcomp_stats = {
49         { "attempts",                   KSTAT_DATA_UINT64 },
50         { "empty",                      KSTAT_DATA_UINT64 },
51         { "skipped_minblocksize",       KSTAT_DATA_UINT64 },
52         { "skipped_insufficient_gain",  KSTAT_DATA_UINT64 }
53 };
54
55 #define ZCOMPSTAT_INCR(stat, val) \
56         atomic_add_64(&zcomp_stats.stat.value.ui64, (val));
57
58 #define ZCOMPSTAT_BUMP(stat)            ZCOMPSTAT_INCR(stat, 1);
59
60 kstat_t         *zcomp_ksp;
61
62 /*
63  * Compression vectors.
64  */
65
66 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
67         {NULL,                  NULL,                   0,      "inherit"},
68         {NULL,                  NULL,                   0,      "on"},
69         {NULL,                  NULL,                   0,      "uncompressed"},
70         {lzjb_compress,         lzjb_decompress,        0,      "lzjb"},
71         {NULL,                  NULL,                   0,      "empty"},
72         {gzip_compress,         gzip_decompress,        1,      "gzip-1"},
73         {gzip_compress,         gzip_decompress,        2,      "gzip-2"},
74         {gzip_compress,         gzip_decompress,        3,      "gzip-3"},
75         {gzip_compress,         gzip_decompress,        4,      "gzip-4"},
76         {gzip_compress,         gzip_decompress,        5,      "gzip-5"},
77         {gzip_compress,         gzip_decompress,        6,      "gzip-6"},
78         {gzip_compress,         gzip_decompress,        7,      "gzip-7"},
79         {gzip_compress,         gzip_decompress,        8,      "gzip-8"},
80         {gzip_compress,         gzip_decompress,        9,      "gzip-9"},
81         {zle_compress,          zle_decompress,         64,     "zle"},
82         {lz4_compress,          lz4_decompress,         0,      "lz4"},
83 };
84
85 enum zio_compress
86 zio_compress_select(enum zio_compress child, enum zio_compress parent)
87 {
88         ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
89         ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
90         ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON);
91
92         if (child == ZIO_COMPRESS_INHERIT)
93                 return (parent);
94
95         if (child == ZIO_COMPRESS_ON)
96                 return (ZIO_COMPRESS_ON_VALUE);
97
98         return (child);
99 }
100
101 size_t
102 zio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len,
103     size_t minblocksize)
104 {
105         uint64_t *word, *word_end;
106         size_t c_len, d_len, r_len;
107         zio_compress_info_t *ci = &zio_compress_table[c];
108
109         ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
110         ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
111
112         ZCOMPSTAT_BUMP(zcompstat_attempts);
113
114         /*
115          * If the data is all zeroes, we don't even need to allocate
116          * a block for it.  We indicate this by returning zero size.
117          */
118         word_end = (uint64_t *)((char *)src + s_len);
119         for (word = src; word < word_end; word++)
120                 if (*word != 0)
121                         break;
122
123         if (word == word_end) {
124                 ZCOMPSTAT_BUMP(zcompstat_empty);
125                 return (0);
126         }
127
128         if (c == ZIO_COMPRESS_EMPTY)
129                 return (s_len);
130
131         /* Compress at least 12.5% */
132         d_len = P2ALIGN(s_len - (s_len >> 3), minblocksize);
133         if (d_len == 0) {
134                 ZCOMPSTAT_BUMP(zcompstat_skipped_minblocksize);
135                 return (s_len);
136         }
137
138         c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level);
139
140         if (c_len > d_len) {
141                 ZCOMPSTAT_BUMP(zcompstat_skipped_insufficient_gain);
142                 return (s_len);
143         }
144
145         /*
146          * Cool.  We compressed at least as much as we were hoping to.
147          * For both security and repeatability, pad out the last sector.
148          */
149         r_len = P2ROUNDUP(c_len, minblocksize);
150         if (r_len > c_len) {
151                 bzero((char *)dst + c_len, r_len - c_len);
152                 c_len = r_len;
153         }
154
155         ASSERT3U(c_len, <=, d_len);
156         ASSERT(P2PHASE(c_len, minblocksize) == 0);
157
158         return (c_len);
159 }
160
161 int
162 zio_decompress_data(enum zio_compress c, void *src, void *dst,
163     size_t s_len, size_t d_len)
164 {
165         zio_compress_info_t *ci = &zio_compress_table[c];
166
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 void
174 zio_compress_init(void)
175 {
176
177         zcomp_ksp = kstat_create("zfs", 0, "zcompstats", "misc",
178             KSTAT_TYPE_NAMED, sizeof (zcomp_stats) / sizeof (kstat_named_t),
179             KSTAT_FLAG_VIRTUAL);
180
181         if (zcomp_ksp != NULL) {
182                 zcomp_ksp->ks_data = &zcomp_stats;
183                 kstat_install(zcomp_ksp);
184         }
185 }
186
187 void
188 zio_compress_fini(void)
189 {
190         if (zcomp_ksp != NULL) {
191                 kstat_delete(zcomp_ksp);
192                 zcomp_ksp = NULL;
193         }
194 }