150476Speter/*
282486Sbde * CDDL HEADER START
321611Swosch *
421611Swosch * The contents of this file are subject to the terms of the
521611Swosch * Common Development and Distribution License (the "License").
618052Sbde * You may not use this file except in compliance with the License.
794940Sru *
894940Sru * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
994940Sru * or http://www.opensolaris.org/os/licensing.
1094940Sru * See the License for the specific language governing permissions
1136494Sbde * and limitations under the License.
1218052Sbde *
1336494Sbde * When distributing Covered Code, include this CDDL HEADER in each
1476576Smarkm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1539412Sphk * If applicable, add the following below this CDDL HEADER, with the
1679495Sobrien * fields enclosed by brackets "[]" replaced with your own identifying
1736494Sbde * information: Portions Copyright [yyyy] [name of copyright owner]
1876515Sbde *
1969227Sbrian * CDDL HEADER END
2036494Sbde */
2139259Sgibbs
2236575Speter/*
2376515Sbde * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2436494Sbde * Use is subject to license terms.
2536494Sbde */
2657461Smarkm/*
2736494Sbde * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
2836575Speter */
2976515Sbde
3039259Sgibbs/*
3136494Sbde * Copyright (c) 2013 by Delphix. All rights reserved.
3236494Sbde */
3336494Sbde
3474535Sdes#include <sys/zfs_context.h>
3518052Sbde#include <sys/compress.h>
3676515Sbde#include <sys/kstat.h>
3736494Sbde#include <sys/spa.h>
3876515Sbde#include <sys/zfeature.h>
3936494Sbde#include <sys/zio.h>
4036494Sbde#include <sys/zio_compress.h>
4194280Sru
4247570Sachetypedef struct zcomp_stats {
4357538Sshin	kstat_named_t zcompstat_attempts;
4436494Sbde	kstat_named_t zcompstat_empty;
4567523Sarchie	kstat_named_t zcompstat_skipped_insufficient_gain;
4636575Speter} zcomp_stats_t;
4736575Speter
4876576Smarkmstatic zcomp_stats_t zcomp_stats = {
4936494Sbde	{ "attempts",			KSTAT_DATA_UINT64 },
5036494Sbde	{ "empty",			KSTAT_DATA_UINT64 },
5136494Sbde	{ "skipped_insufficient_gain",	KSTAT_DATA_UINT64 }
5265916Sache};
5336494Sbde
5436494Sbde#define	ZCOMPSTAT_INCR(stat, val) \
5576515Sbde	atomic_add_64(&zcomp_stats.stat.value.ui64, (val));
5690796Sgshapiro
5790796Sgshapiro#define	ZCOMPSTAT_BUMP(stat)		ZCOMPSTAT_INCR(stat, 1);
5890796Sgshapiro
5936494Sbdekstat_t		*zcomp_ksp;
6065916Sache
6152251Sbp/*
6236494Sbde * Compression vectors.
6352419Sjulian */
6436494Sbde
6536494Sbdezio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
6642916Sjdp	{NULL,			NULL,			0,	"inherit"},
6742916Sjdp	{NULL,			NULL,			0,	"on"},
6842916Sjdp	{NULL,			NULL,			0,	"uncompressed"},
6959770Sbde	{lzjb_compress,		lzjb_decompress,	0,	"lzjb"},
7042916Sjdp	{NULL,			NULL,			0,	"empty"},
7142916Sjdp	{gzip_compress,		gzip_decompress,	1,	"gzip-1"},
7276576Smarkm	{gzip_compress,		gzip_decompress,	2,	"gzip-2"},
7342916Sjdp	{gzip_compress,		gzip_decompress,	3,	"gzip-3"},
7476576Smarkm	{gzip_compress,		gzip_decompress,	4,	"gzip-4"},
7576576Smarkm	{gzip_compress,		gzip_decompress,	5,	"gzip-5"},
7642916Sjdp	{gzip_compress,		gzip_decompress,	6,	"gzip-6"},
7776576Smarkm	{gzip_compress,		gzip_decompress,	7,	"gzip-7"},
7877866Smarkm	{gzip_compress,		gzip_decompress,	8,	"gzip-8"},
7977866Smarkm	{gzip_compress,		gzip_decompress,	9,	"gzip-9"},
8076576Smarkm	{zle_compress,		zle_decompress,		64,	"zle"},
8177866Smarkm	{lz4_compress,		lz4_decompress,		0,	"lz4"},
8276576Smarkm};
8376576Smarkm
8489705Sruenum zio_compress
8594714Sdeszio_compress_select(spa_t *spa, enum zio_compress child,
8689705Sru    enum zio_compress parent)
8794714Sdes{
8889705Sru	enum zio_compress result;
8989705Sru
9089705Sru	ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
9142916Sjdp	ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
9289705Sru	ASSERT(parent != ZIO_COMPRESS_INHERIT);
9342916Sjdp
9476515Sbde	result = child;
9536494Sbde	if (result == ZIO_COMPRESS_INHERIT)
9636494Sbde		result = parent;
9739020Smarkm
9836494Sbde	if (result == ZIO_COMPRESS_ON) {
9941231Sjdp		if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
10036494Sbde			result = ZIO_COMPRESS_LZ4_ON_VALUE;
10136494Sbde		else
10277866Smarkm			result = ZIO_COMPRESS_LEGACY_ON_VALUE;
10336494Sbde	}
10474840Sken
10588142Sru	return (result);
10689705Sru}
10766913Sgshapiro
10836494Sbdesize_t
10941231Sjdpzio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len)
11036494Sbde{
11118052Sbde	uint64_t *word, *word_end;
11276515Sbde	size_t c_len, d_len;
113101224Srwatson	zio_compress_info_t *ci = &zio_compress_table[c];
11436494Sbde
11593351Sjoe	ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
11676515Sbde	ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
11744757Smarkm
11836494Sbde	ZCOMPSTAT_BUMP(zcompstat_attempts);
11936494Sbde
12094578Sdes	/*
12136494Sbde	 * If the data is all zeroes, we don't even need to allocate
122	 * a block for it.  We indicate this by returning zero size.
123	 */
124	word_end = (uint64_t *)((char *)src + s_len);
125	for (word = src; word < word_end; word++)
126		if (*word != 0)
127			break;
128
129	if (word == word_end) {
130		ZCOMPSTAT_BUMP(zcompstat_empty);
131 		return (0);
132	}
133
134	if (c == ZIO_COMPRESS_EMPTY)
135		return (s_len);
136
137	/* Compress at least 12.5% */
138	d_len = s_len - (s_len >> 3);
139	c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level);
140
141	if (c_len > d_len) {
142		ZCOMPSTAT_BUMP(zcompstat_skipped_insufficient_gain);
143		return (s_len);
144	}
145
146	ASSERT3U(c_len, <=, d_len);
147	return (c_len);
148}
149
150int
151zio_decompress_data(enum zio_compress c, void *src, void *dst,
152    size_t s_len, size_t d_len)
153{
154	zio_compress_info_t *ci = &zio_compress_table[c];
155
156	if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
157		return (SET_ERROR(EINVAL));
158
159	return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
160}
161
162void
163zio_compress_init(void)
164{
165
166	zcomp_ksp = kstat_create("zfs", 0, "zcompstats", "misc",
167	    KSTAT_TYPE_NAMED, sizeof (zcomp_stats) / sizeof (kstat_named_t),
168	    KSTAT_FLAG_VIRTUAL);
169
170	if (zcomp_ksp != NULL) {
171		zcomp_ksp->ks_data = &zcomp_stats;
172		kstat_install(zcomp_ksp);
173	}
174}
175
176void
177zio_compress_fini(void)
178{
179	if (zcomp_ksp != NULL) {
180		kstat_delete(zcomp_ksp);
181		zcomp_ksp = NULL;
182	}
183}
184