xref: /titanic_52/usr/src/uts/common/fs/zfs/zio_compress.c (revision 911106dfb16696472af8c1b7b4c554a829354fa8)
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 2007 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,			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"},
54 };
55 
56 uint8_t
57 zio_compress_select(uint8_t child, uint8_t parent)
58 {
59 	ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
60 	ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
61 	ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON);
62 
63 	if (child == ZIO_COMPRESS_INHERIT)
64 		return (parent);
65 
66 	if (child == ZIO_COMPRESS_ON)
67 		return (ZIO_COMPRESS_ON_VALUE);
68 
69 	return (child);
70 }
71 
72 int
73 zio_compress_data(int cpfunc, void *src, uint64_t srcsize, void **destp,
74     uint64_t *destsizep, uint64_t *destbufsizep)
75 {
76 	uint64_t *word, *word_end;
77 	uint64_t ciosize, gapsize, destbufsize;
78 	zio_compress_info_t *ci = &zio_compress_table[cpfunc];
79 	char *dest;
80 	uint_t allzero;
81 
82 	ASSERT((uint_t)cpfunc < ZIO_COMPRESS_FUNCTIONS);
83 	ASSERT((uint_t)cpfunc == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
84 
85 	/*
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.
88 	 */
89 	allzero = 1;
90 	word = src;
91 	word_end = (uint64_t *)(uintptr_t)((uintptr_t)word + srcsize);
92 	while (word < word_end) {
93 		if (*word++ != 0) {
94 			allzero = 0;
95 			break;
96 		}
97 	}
98 	if (allzero) {
99 		*destp = NULL;
100 		*destsizep = 0;
101 		*destbufsizep = 0;
102 		return (1);
103 	}
104 
105 	if (cpfunc == ZIO_COMPRESS_EMPTY)
106 		return (0);
107 
108 	/* Compress at least 12.5% */
109 	destbufsize = P2ALIGN(srcsize - (srcsize >> 3), SPA_MINBLOCKSIZE);
110 	if (destbufsize == 0)
111 		return (0);
112 	dest = zio_buf_alloc(destbufsize);
113 
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);
118 		return (0);
119 	}
120 
121 	/* Cool.  We compressed at least as much as we were hoping to. */
122 
123 	/* For security, make sure we don't write random heap crap to disk */
124 	gapsize = P2ROUNDUP(ciosize, SPA_MINBLOCKSIZE) - ciosize;
125 	if (gapsize != 0) {
126 		bzero(dest + ciosize, gapsize);
127 		ciosize += gapsize;
128 	}
129 
130 	ASSERT3U(ciosize, <=, destbufsize);
131 	ASSERT(P2PHASE(ciosize, SPA_MINBLOCKSIZE) == 0);
132 	*destp = dest;
133 	*destsizep = ciosize;
134 	*destbufsizep = destbufsize;
135 
136 	return (1);
137 }
138 
139 int
140 zio_decompress_data(int cpfunc, void *src, uint64_t srcsize,
141 	void *dest, uint64_t destsize)
142 {
143 	zio_compress_info_t *ci = &zio_compress_table[cpfunc];
144 
145 	ASSERT((uint_t)cpfunc < ZIO_COMPRESS_FUNCTIONS);
146 
147 	return (ci->ci_decompress(src, dest, srcsize, destsize, ci->ci_level));
148 }
149