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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/zfs_context.h>
30 #include <sys/compress.h>
33 #include <sys/zio_compress.h>
36 * Compression vectors.
39 zio_compress_info_t zio_compress_table
[ZIO_COMPRESS_FUNCTIONS
] = {
40 {NULL
, NULL
, 0, "inherit"},
41 {NULL
, NULL
, 0, "on"},
42 {NULL
, NULL
, 0, "uncompressed"},
43 {lzjb_compress
, lzjb_decompress
, 0, "lzjb"},
44 {NULL
, NULL
, 0, "empty"},
45 {gzip_compress
, gzip_decompress
, 1, "gzip-1"},
46 {gzip_compress
, gzip_decompress
, 2, "gzip-2"},
47 {gzip_compress
, gzip_decompress
, 3, "gzip-3"},
48 {gzip_compress
, gzip_decompress
, 4, "gzip-4"},
49 {gzip_compress
, gzip_decompress
, 5, "gzip-5"},
50 {gzip_compress
, gzip_decompress
, 6, "gzip-6"},
51 {gzip_compress
, gzip_decompress
, 7, "gzip-7"},
52 {gzip_compress
, gzip_decompress
, 8, "gzip-8"},
53 {gzip_compress
, gzip_decompress
, 9, "gzip-9"},
57 zio_compress_select(uint8_t child
, uint8_t parent
)
59 ASSERT(child
< ZIO_COMPRESS_FUNCTIONS
);
60 ASSERT(parent
< ZIO_COMPRESS_FUNCTIONS
);
61 ASSERT(parent
!= ZIO_COMPRESS_INHERIT
&& parent
!= ZIO_COMPRESS_ON
);
63 if (child
== ZIO_COMPRESS_INHERIT
)
66 if (child
== ZIO_COMPRESS_ON
)
67 return (ZIO_COMPRESS_ON_VALUE
);
73 zio_compress_data(int cpfunc
, void *src
, uint64_t srcsize
, void **destp
,
74 uint64_t *destsizep
, uint64_t *destbufsizep
)
76 uint64_t *word
, *word_end
;
77 uint64_t ciosize
, gapsize
, destbufsize
;
78 zio_compress_info_t
*ci
= &zio_compress_table
[cpfunc
];
82 ASSERT((uint_t
)cpfunc
< ZIO_COMPRESS_FUNCTIONS
);
83 ASSERT((uint_t
)cpfunc
== ZIO_COMPRESS_EMPTY
|| ci
->ci_compress
!= NULL
);
86 * If the data is all zeroes, we don't even need to allocate
87 * a block for it. We indicate this by setting *destsizep = 0.
91 word_end
= (uint64_t *)(uintptr_t)((uintptr_t)word
+ srcsize
);
92 while (word
< word_end
) {
105 if (cpfunc
== ZIO_COMPRESS_EMPTY
)
108 /* Compress at least 12.5% */
109 destbufsize
= P2ALIGN(srcsize
- (srcsize
>> 3), SPA_MINBLOCKSIZE
);
110 if (destbufsize
== 0)
112 dest
= zio_buf_alloc(destbufsize
);
114 ciosize
= ci
->ci_compress(src
, dest
, (size_t)srcsize
,
115 (size_t)destbufsize
, ci
->ci_level
);
116 if (ciosize
> destbufsize
) {
117 zio_buf_free(dest
, destbufsize
);
121 /* Cool. We compressed at least as much as we were hoping to. */
123 /* For security, make sure we don't write random heap crap to disk */
124 gapsize
= P2ROUNDUP(ciosize
, SPA_MINBLOCKSIZE
) - ciosize
;
126 bzero(dest
+ ciosize
, gapsize
);
130 ASSERT3U(ciosize
, <=, destbufsize
);
131 ASSERT(P2PHASE(ciosize
, SPA_MINBLOCKSIZE
) == 0);
133 *destsizep
= ciosize
;
134 *destbufsizep
= destbufsize
;
140 zio_decompress_data(int cpfunc
, void *src
, uint64_t srcsize
,
141 void *dest
, uint64_t destsize
)
143 zio_compress_info_t
*ci
= &zio_compress_table
[cpfunc
];
145 ASSERT((uint_t
)cpfunc
< ZIO_COMPRESS_FUNCTIONS
);
147 return (ci
->ci_decompress(src
, dest
, srcsize
, destsize
, ci
->ci_level
));