]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zio_compress.c
Update core ZFS code from build 121 to build 141.
[mirror_zfs-debian.git] / module / zfs / zio_compress.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
24 * Use is subject to license terms.
25 */
26
34dc7c2f
BB
27#include <sys/zfs_context.h>
28#include <sys/compress.h>
29#include <sys/spa.h>
30#include <sys/zio.h>
31#include <sys/zio_compress.h>
32
33/*
34 * Compression vectors.
35 */
36
37zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
38 {NULL, NULL, 0, "inherit"},
39 {NULL, NULL, 0, "on"},
40 {NULL, NULL, 0, "uncompressed"},
41 {lzjb_compress, lzjb_decompress, 0, "lzjb"},
42 {NULL, NULL, 0, "empty"},
43 {gzip_compress, gzip_decompress, 1, "gzip-1"},
44 {gzip_compress, gzip_decompress, 2, "gzip-2"},
45 {gzip_compress, gzip_decompress, 3, "gzip-3"},
46 {gzip_compress, gzip_decompress, 4, "gzip-4"},
47 {gzip_compress, gzip_decompress, 5, "gzip-5"},
48 {gzip_compress, gzip_decompress, 6, "gzip-6"},
49 {gzip_compress, gzip_decompress, 7, "gzip-7"},
50 {gzip_compress, gzip_decompress, 8, "gzip-8"},
51 {gzip_compress, gzip_decompress, 9, "gzip-9"},
428870ff 52 {zle_compress, zle_decompress, 64, "zle"},
34dc7c2f
BB
53};
54
428870ff
BB
55enum zio_compress
56zio_compress_select(enum zio_compress child, enum zio_compress parent)
34dc7c2f
BB
57{
58 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
59 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
60 ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON);
61
62 if (child == ZIO_COMPRESS_INHERIT)
63 return (parent);
64
65 if (child == ZIO_COMPRESS_ON)
66 return (ZIO_COMPRESS_ON_VALUE);
67
68 return (child);
69}
70
428870ff
BB
71size_t
72zio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len)
34dc7c2f
BB
73{
74 uint64_t *word, *word_end;
428870ff
BB
75 size_t c_len, d_len, r_len;
76 zio_compress_info_t *ci = &zio_compress_table[c];
34dc7c2f 77
428870ff
BB
78 ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
79 ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
34dc7c2f
BB
80
81 /*
82 * If the data is all zeroes, we don't even need to allocate
428870ff 83 * a block for it. We indicate this by returning zero size.
34dc7c2f 84 */
428870ff
BB
85 word_end = (uint64_t *)((char *)src + s_len);
86 for (word = src; word < word_end; word++)
87 if (*word != 0)
34dc7c2f 88 break;
34dc7c2f 89
428870ff 90 if (word == word_end)
34dc7c2f
BB
91 return (0);
92
428870ff
BB
93 if (c == ZIO_COMPRESS_EMPTY)
94 return (s_len);
95
34dc7c2f 96 /* Compress at least 12.5% */
428870ff
BB
97 d_len = P2ALIGN(s_len - (s_len >> 3), (size_t)SPA_MINBLOCKSIZE);
98 if (d_len == 0)
99 return (s_len);
34dc7c2f 100
428870ff 101 c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level);
34dc7c2f 102
428870ff
BB
103 if (c_len > d_len)
104 return (s_len);
34dc7c2f 105
428870ff
BB
106 /*
107 * Cool. We compressed at least as much as we were hoping to.
108 * For both security and repeatability, pad out the last sector.
109 */
110 r_len = P2ROUNDUP(c_len, (size_t)SPA_MINBLOCKSIZE);
111 if (r_len > c_len) {
112 bzero((char *)dst + c_len, r_len - c_len);
113 c_len = r_len;
34dc7c2f
BB
114 }
115
428870ff
BB
116 ASSERT3U(c_len, <=, d_len);
117 ASSERT(P2PHASE(c_len, (size_t)SPA_MINBLOCKSIZE) == 0);
34dc7c2f 118
428870ff 119 return (c_len);
34dc7c2f
BB
120}
121
122int
428870ff
BB
123zio_decompress_data(enum zio_compress c, void *src, void *dst,
124 size_t s_len, size_t d_len)
34dc7c2f 125{
428870ff 126 zio_compress_info_t *ci = &zio_compress_table[c];
34dc7c2f 127
428870ff
BB
128 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
129 return (EINVAL);
34dc7c2f 130
428870ff 131 return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
34dc7c2f 132}