]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zio_compress.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.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 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/zio.h>
38 #include <sys/zio_compress.h>
39
40 /*
41 * Compression vectors.
42 */
43
44 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
45 {NULL, NULL, 0, "inherit"},
46 {NULL, NULL, 0, "on"},
47 {NULL, NULL, 0, "uncompressed"},
48 {lzjb_compress, lzjb_decompress, 0, "lzjb"},
49 {NULL, NULL, 0, "empty"},
50 {gzip_compress, gzip_decompress, 1, "gzip-1"},
51 {gzip_compress, gzip_decompress, 2, "gzip-2"},
52 {gzip_compress, gzip_decompress, 3, "gzip-3"},
53 {gzip_compress, gzip_decompress, 4, "gzip-4"},
54 {gzip_compress, gzip_decompress, 5, "gzip-5"},
55 {gzip_compress, gzip_decompress, 6, "gzip-6"},
56 {gzip_compress, gzip_decompress, 7, "gzip-7"},
57 {gzip_compress, gzip_decompress, 8, "gzip-8"},
58 {gzip_compress, gzip_decompress, 9, "gzip-9"},
59 {zle_compress, zle_decompress, 64, "zle"},
60 {lz4_compress_zfs, lz4_decompress_zfs, 0, "lz4"},
61 };
62
63 enum zio_compress
64 zio_compress_select(enum zio_compress child, enum zio_compress parent)
65 {
66 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
67 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
68 ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON);
69
70 if (child == ZIO_COMPRESS_INHERIT)
71 return (parent);
72
73 if (child == ZIO_COMPRESS_ON)
74 return (ZIO_COMPRESS_ON_VALUE);
75
76 return (child);
77 }
78
79 size_t
80 zio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len)
81 {
82 uint64_t *word, *word_end;
83 size_t c_len, d_len;
84 zio_compress_info_t *ci = &zio_compress_table[c];
85
86 ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
87 ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
88
89 /*
90 * If the data is all zeroes, we don't even need to allocate
91 * a block for it. We indicate this by returning zero size.
92 */
93 word_end = (uint64_t *)((char *)src + s_len);
94 for (word = src; word < word_end; word++)
95 if (*word != 0)
96 break;
97
98 if (word == word_end)
99 return (0);
100
101 if (c == ZIO_COMPRESS_EMPTY)
102 return (s_len);
103
104 /* Compress at least 12.5% */
105 d_len = s_len - (s_len >> 3);
106 c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level);
107
108 if (c_len > d_len)
109 return (s_len);
110
111 ASSERT3U(c_len, <=, d_len);
112 return (c_len);
113 }
114
115 int
116 zio_decompress_data(enum zio_compress c, void *src, void *dst,
117 size_t s_len, size_t d_len)
118 {
119 zio_compress_info_t *ci = &zio_compress_table[c];
120
121 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
122 return (SET_ERROR(EINVAL));
123
124 return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
125 }