xref: /titanic_41/usr/src/uts/common/fs/zfs/zio_compress.c (revision 058561cbaa119a6f2659bc27ef343e1b47266bb2)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/compress.h>
30 #include <sys/spa.h>
31 #include <sys/zio.h>
32 #include <sys/zio_compress.h>
33 
34 /*
35  * Compression vectors.
36  */
37 
38 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
39 	{NULL,			NULL,			"inherit"},
40 	{NULL,			NULL,			"on"},
41 	{NULL,			NULL,			"uncompressed"},
42 	{lzjb_compress,		lzjb_decompress,	"lzjb"},
43 	{NULL,			NULL,			"empty"},
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((uint_t)cpfunc == ZIO_COMPRESS_EMPTY || 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 	if (cpfunc == ZIO_COMPRESS_EMPTY)
96 		return (0);
97 
98 	/* Compress at least 12.5% */
99 	destbufsize = P2ALIGN(srcsize - (srcsize >> 3), SPA_MINBLOCKSIZE);
100 	if (destbufsize == 0)
101 		return (0);
102 	dest = zio_buf_alloc(destbufsize);
103 
104 	ciosize = ci->ci_compress(src, dest, (size_t)srcsize,
105 	    (size_t)destbufsize);
106 	if (ciosize > destbufsize) {
107 		zio_buf_free(dest, destbufsize);
108 		return (0);
109 	}
110 
111 	/* Cool.  We compressed at least as much as we were hoping to. */
112 
113 	/* For security, make sure we don't write random heap crap to disk */
114 	gapsize = P2ROUNDUP(ciosize, SPA_MINBLOCKSIZE) - ciosize;
115 	if (gapsize != 0) {
116 		bzero(dest + ciosize, gapsize);
117 		ciosize += gapsize;
118 	}
119 
120 	ASSERT3U(ciosize, <=, destbufsize);
121 	ASSERT(P2PHASE(ciosize, SPA_MINBLOCKSIZE) == 0);
122 	*destp = dest;
123 	*destsizep = ciosize;
124 	*destbufsizep = destbufsize;
125 
126 	return (1);
127 }
128 
129 int
130 zio_decompress_data(int cpfunc, void *src, uint64_t srcsize,
131 	void *dest, uint64_t destsize)
132 {
133 	ASSERT((uint_t)cpfunc < ZIO_COMPRESS_FUNCTIONS);
134 
135 	return (zio_compress_table[cpfunc].ci_decompress(src, dest,
136 	    srcsize, destsize));
137 }
138