xref: /illumos-gate/usr/src/uts/common/fs/zfs/zio_compress.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/zfs_context.h>
30 #include <sys/compress.h>
31 #include <sys/spa.h>
32 #include <sys/zio.h>
33 #include <sys/zio_compress.h>
34 
35 /*
36  * Compression vectors.
37  */
38 
39 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
40 	NULL,			NULL,			"inherit",
41 	NULL,			NULL,			"on",
42 	NULL,			NULL,			"uncompressed",
43 	lzjb_compress,		lzjb_decompress,	"lzjb",
44 };
45 
46 uint8_t
47 zio_compress_select(uint8_t child, uint8_t parent)
48 {
49 	ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
50 	ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
51 	ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON);
52 
53 	if (child == ZIO_COMPRESS_INHERIT)
54 		return (parent);
55 
56 	if (child == ZIO_COMPRESS_ON)
57 		return (ZIO_COMPRESS_ON_VALUE);
58 
59 	return (child);
60 }
61 
62 int
63 zio_compress_data(int cpfunc, void *src, uint64_t srcsize, void **destp,
64     uint64_t *destsizep, uint64_t *destbufsizep)
65 {
66 	uint64_t *word, *word_end;
67 	uint64_t ciosize, gapsize, destbufsize;
68 	zio_compress_info_t *ci = &zio_compress_table[cpfunc];
69 	char *dest;
70 	uint_t allzero;
71 
72 	ASSERT((uint_t)cpfunc < ZIO_COMPRESS_FUNCTIONS);
73 	ASSERT(ci->ci_compress != NULL);
74 
75 	/*
76 	 * If the data is all zeroes, we don't even need to allocate
77 	 * a block for it.  We indicate this by setting *destsizep = 0.
78 	 */
79 	allzero = 1;
80 	word = src;
81 	word_end = (uint64_t *)(uintptr_t)((uintptr_t)word + srcsize);
82 	while (word < word_end) {
83 		if (*word++ != 0) {
84 			allzero = 0;
85 			break;
86 		}
87 	}
88 	if (allzero) {
89 		*destp = NULL;
90 		*destsizep = 0;
91 		*destbufsizep = 0;
92 		return (1);
93 	}
94 
95 	/* Compress at least 12.5% */
96 	destbufsize = P2ALIGN(srcsize - (srcsize >> 3), SPA_MINBLOCKSIZE);
97 	if (destbufsize == 0)
98 		return (0);
99 	dest = zio_buf_alloc(destbufsize);
100 
101 	ciosize = ci->ci_compress(src, dest, (size_t)srcsize,
102 	    (size_t)destbufsize);
103 	if (ciosize > destbufsize) {
104 		zio_buf_free(dest, destbufsize);
105 		return (0);
106 	}
107 
108 	/* Cool.  We compressed at least as much as we were hoping to. */
109 
110 	/* For security, make sure we don't write random heap crap to disk */
111 	gapsize = P2ROUNDUP(ciosize, SPA_MINBLOCKSIZE) - ciosize;
112 	if (gapsize != 0) {
113 		bzero(dest + ciosize, gapsize);
114 		ciosize += gapsize;
115 	}
116 
117 	ASSERT3U(ciosize, <=, destbufsize);
118 	ASSERT(P2PHASE(ciosize, SPA_MINBLOCKSIZE) == 0);
119 	*destp = dest;
120 	*destsizep = ciosize;
121 	*destbufsizep = destbufsize;
122 
123 	return (1);
124 }
125 
126 int
127 zio_decompress_data(int cpfunc, void *src, uint64_t srcsize,
128 	void *dest, uint64_t destsize)
129 {
130 	ASSERT((uint_t)cpfunc < ZIO_COMPRESS_FUNCTIONS);
131 
132 	return (zio_compress_table[cpfunc].ci_decompress(src, dest,
133 	    srcsize, destsize));
134 }
135