]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zio_compress.c
Undo c89 workarounds to match with upstream
[mirror_zfs.git] / module / 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, 2016 by Delphix. All rights reserved.
32 */
33
34 #include <sys/zfs_context.h>
35 #include <sys/compress.h>
36 #include <sys/spa.h>
37 #include <sys/zfeature.h>
38 #include <sys/zio.h>
39 #include <sys/zio_compress.h>
40
41 /*
42 * Compression vectors.
43 */
44 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
45 {"inherit", 0, NULL, NULL},
46 {"on", 0, NULL, NULL},
47 {"uncompressed", 0, NULL, NULL},
48 {"lzjb", 0, lzjb_compress, lzjb_decompress},
49 {"empty", 0, NULL, NULL},
50 {"gzip-1", 1, gzip_compress, gzip_decompress},
51 {"gzip-2", 2, gzip_compress, gzip_decompress},
52 {"gzip-3", 3, gzip_compress, gzip_decompress},
53 {"gzip-4", 4, gzip_compress, gzip_decompress},
54 {"gzip-5", 5, gzip_compress, gzip_decompress},
55 {"gzip-6", 6, gzip_compress, gzip_decompress},
56 {"gzip-7", 7, gzip_compress, gzip_decompress},
57 {"gzip-8", 8, gzip_compress, gzip_decompress},
58 {"gzip-9", 9, gzip_compress, gzip_decompress},
59 {"zle", 64, zle_compress, zle_decompress},
60 {"lz4", 0, lz4_compress_zfs, lz4_decompress_zfs}
61 };
62
63 enum zio_compress
64 zio_compress_select(spa_t *spa, enum zio_compress child,
65 enum zio_compress parent)
66 {
67 enum zio_compress result;
68
69 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
70 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
71 ASSERT(parent != ZIO_COMPRESS_INHERIT);
72
73 result = child;
74 if (result == ZIO_COMPRESS_INHERIT)
75 result = parent;
76
77 if (result == ZIO_COMPRESS_ON) {
78 if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
79 result = ZIO_COMPRESS_LZ4_ON_VALUE;
80 else
81 result = ZIO_COMPRESS_LEGACY_ON_VALUE;
82 }
83
84 return (result);
85 }
86
87 /*ARGSUSED*/
88 static int
89 zio_compress_zeroed_cb(void *data, size_t len, void *private)
90 {
91 uint64_t *end = (uint64_t *)((char *)data + len);
92 for (uint64_t *word = (uint64_t *)data; word < end; word++)
93 if (*word != 0)
94 return (1);
95
96 return (0);
97 }
98
99 size_t
100 zio_compress_data(enum zio_compress c, abd_t *src, void *dst, size_t s_len)
101 {
102 size_t c_len, d_len;
103 zio_compress_info_t *ci = &zio_compress_table[c];
104
105 ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
106 ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
107
108 /*
109 * If the data is all zeroes, we don't even need to allocate
110 * a block for it. We indicate this by returning zero size.
111 */
112 if (abd_iterate_func(src, 0, s_len, zio_compress_zeroed_cb, NULL) == 0)
113 return (0);
114
115 if (c == ZIO_COMPRESS_EMPTY)
116 return (s_len);
117
118 /* Compress at least 12.5% */
119 d_len = s_len - (s_len >> 3);
120
121 /* No compression algorithms can read from ABDs directly */
122 void *tmp = abd_borrow_buf_copy(src, s_len);
123 c_len = ci->ci_compress(tmp, dst, s_len, d_len, ci->ci_level);
124 abd_return_buf(src, tmp, s_len);
125
126 if (c_len > d_len)
127 return (s_len);
128
129 ASSERT3U(c_len, <=, d_len);
130 return (c_len);
131 }
132
133 int
134 zio_decompress_data_buf(enum zio_compress c, void *src, void *dst,
135 size_t s_len, size_t d_len)
136 {
137 zio_compress_info_t *ci = &zio_compress_table[c];
138 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
139 return (SET_ERROR(EINVAL));
140
141 return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
142 }
143
144 int
145 zio_decompress_data(enum zio_compress c, abd_t *src, void *dst,
146 size_t s_len, size_t d_len)
147 {
148 void *tmp = abd_borrow_buf_copy(src, s_len);
149 int ret = zio_decompress_data_buf(c, tmp, dst, s_len, d_len);
150 abd_return_buf(src, tmp, s_len);
151
152 return (ret);
153 }