]>
Commit | Line | Data |
---|---|---|
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 | */ | |
9759c60f ED |
26 | /* |
27 | * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. | |
28 | */ | |
34dc7c2f | 29 | |
a08ee875 LG |
30 | /* |
31 | * Copyright (c) 2013 by Delphix. All rights reserved. | |
32 | */ | |
33 | ||
34dc7c2f BB |
34 | #include <sys/zfs_context.h> |
35 | #include <sys/compress.h> | |
36 | #include <sys/spa.h> | |
e10b0808 | 37 | #include <sys/zfeature.h> |
34dc7c2f BB |
38 | #include <sys/zio.h> |
39 | #include <sys/zio_compress.h> | |
40 | ||
41 | /* | |
42 | * Compression vectors. | |
43 | */ | |
44 | ||
45 | zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = { | |
46 | {NULL, NULL, 0, "inherit"}, | |
47 | {NULL, NULL, 0, "on"}, | |
48 | {NULL, NULL, 0, "uncompressed"}, | |
49 | {lzjb_compress, lzjb_decompress, 0, "lzjb"}, | |
50 | {NULL, NULL, 0, "empty"}, | |
51 | {gzip_compress, gzip_decompress, 1, "gzip-1"}, | |
52 | {gzip_compress, gzip_decompress, 2, "gzip-2"}, | |
53 | {gzip_compress, gzip_decompress, 3, "gzip-3"}, | |
54 | {gzip_compress, gzip_decompress, 4, "gzip-4"}, | |
55 | {gzip_compress, gzip_decompress, 5, "gzip-5"}, | |
56 | {gzip_compress, gzip_decompress, 6, "gzip-6"}, | |
57 | {gzip_compress, gzip_decompress, 7, "gzip-7"}, | |
58 | {gzip_compress, gzip_decompress, 8, "gzip-8"}, | |
59 | {gzip_compress, gzip_decompress, 9, "gzip-9"}, | |
428870ff | 60 | {zle_compress, zle_decompress, 64, "zle"}, |
a08ee875 | 61 | {lz4_compress_zfs, lz4_decompress_zfs, 0, "lz4"}, |
34dc7c2f BB |
62 | }; |
63 | ||
428870ff | 64 | enum zio_compress |
e10b0808 AX |
65 | zio_compress_select(spa_t *spa, enum zio_compress child, |
66 | enum zio_compress parent) | |
34dc7c2f | 67 | { |
e10b0808 AX |
68 | enum zio_compress result; |
69 | ||
34dc7c2f BB |
70 | ASSERT(child < ZIO_COMPRESS_FUNCTIONS); |
71 | ASSERT(parent < ZIO_COMPRESS_FUNCTIONS); | |
e10b0808 | 72 | ASSERT(parent != ZIO_COMPRESS_INHERIT); |
34dc7c2f | 73 | |
e10b0808 AX |
74 | result = child; |
75 | if (result == ZIO_COMPRESS_INHERIT) | |
76 | result = parent; | |
34dc7c2f | 77 | |
e10b0808 AX |
78 | if (result == ZIO_COMPRESS_ON) { |
79 | if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS)) | |
80 | result = ZIO_COMPRESS_LZ4_ON_VALUE; | |
81 | else | |
82 | result = ZIO_COMPRESS_LEGACY_ON_VALUE; | |
83 | } | |
34dc7c2f | 84 | |
e10b0808 | 85 | return (result); |
34dc7c2f BB |
86 | } |
87 | ||
428870ff BB |
88 | size_t |
89 | zio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len) | |
34dc7c2f BB |
90 | { |
91 | uint64_t *word, *word_end; | |
ea04106b | 92 | size_t c_len, d_len; |
428870ff | 93 | zio_compress_info_t *ci = &zio_compress_table[c]; |
34dc7c2f | 94 | |
428870ff BB |
95 | ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS); |
96 | ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL); | |
34dc7c2f BB |
97 | |
98 | /* | |
99 | * If the data is all zeroes, we don't even need to allocate | |
428870ff | 100 | * a block for it. We indicate this by returning zero size. |
34dc7c2f | 101 | */ |
428870ff BB |
102 | word_end = (uint64_t *)((char *)src + s_len); |
103 | for (word = src; word < word_end; word++) | |
104 | if (*word != 0) | |
34dc7c2f | 105 | break; |
34dc7c2f | 106 | |
428870ff | 107 | if (word == word_end) |
34dc7c2f BB |
108 | return (0); |
109 | ||
428870ff BB |
110 | if (c == ZIO_COMPRESS_EMPTY) |
111 | return (s_len); | |
112 | ||
34dc7c2f | 113 | /* Compress at least 12.5% */ |
ea04106b | 114 | d_len = s_len - (s_len >> 3); |
428870ff | 115 | c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level); |
34dc7c2f | 116 | |
428870ff BB |
117 | if (c_len > d_len) |
118 | return (s_len); | |
34dc7c2f | 119 | |
428870ff | 120 | ASSERT3U(c_len, <=, d_len); |
428870ff | 121 | return (c_len); |
34dc7c2f BB |
122 | } |
123 | ||
124 | int | |
428870ff BB |
125 | zio_decompress_data(enum zio_compress c, void *src, void *dst, |
126 | size_t s_len, size_t d_len) | |
34dc7c2f | 127 | { |
428870ff | 128 | zio_compress_info_t *ci = &zio_compress_table[c]; |
34dc7c2f | 129 | |
428870ff | 130 | if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL) |
a08ee875 | 131 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 132 | |
428870ff | 133 | return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level)); |
34dc7c2f | 134 | } |