linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andi Kleen <andi@firstfloor.org>
To: chris.mason@oracle.com
Cc: linux-kernel@vger.kernel.org, linux-btrfs@vger.kernel.org,
	Andi Kleen <ak@linux.intel.com>
Subject: [PATCH 3/3] Add snappy interface to crypto API
Date: Thu, 12 Jan 2012 16:28:50 -0800	[thread overview]
Message-ID: <1326414530-10789-4-git-send-email-andi@firstfloor.org> (raw)
In-Reply-To: <1326414530-10789-1-git-send-email-andi@firstfloor.org>

From: Andi Kleen <ak@linux.intel.com>

Mainly so that ubifs can use it.

Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
 crypto/Kconfig  |    9 +++++
 crypto/Makefile |    1 +
 crypto/snappy.c |   99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100644 crypto/snappy.c

diff --git a/crypto/Kconfig b/crypto/Kconfig
index e6cfe1a..768943dc 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -924,6 +924,15 @@ config CRYPTO_LZO
 	help
 	  This is the LZO algorithm.
 
+config CRYPTO_SNAPPY
+	tristate "Snappy compression algorithm"
+	select CRYPTO_ALGAPI
+	select SNAPPY
+	help
+	  snappy is a faster alternative to the lzo compression algorithm
+	  with comparable compression. It is very fast on 64bit systems, but also
+	  good on 32bit systems. It especially excels at already compressed data.
+
 comment "Random Number Generation"
 
 config CRYPTO_ANSI_CPRNG
diff --git a/crypto/Makefile b/crypto/Makefile
index f638063..8a0e6c6 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
 obj-$(CONFIG_CRYPTO_LZO) += lzo.o
+obj-$(CONFIG_CRYPTO_SNAPPY) += snappy.o
 obj-$(CONFIG_CRYPTO_RNG2) += rng.o
 obj-$(CONFIG_CRYPTO_RNG2) += krng.o
 obj-$(CONFIG_CRYPTO_ANSI_CPRNG) += ansi_cprng.o
diff --git a/crypto/snappy.c b/crypto/snappy.c
new file mode 100644
index 0000000..43a64b9
--- /dev/null
+++ b/crypto/snappy.c
@@ -0,0 +1,99 @@
+/*
+ * Cryptographic API.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/crypto.h>
+#include <linux/vmalloc.h>
+#include <linux/snappy.h>
+
+struct snappy_ctx {
+	struct snappy_env env;
+};
+
+/* Only needed for compression actually */
+static int snappy_init(struct crypto_tfm *tfm)
+{
+	struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
+
+	return snappy_init_env(&ctx->env);
+}
+
+static void snappy_exit(struct crypto_tfm *tfm)
+{
+	struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
+
+	snappy_free_env(&ctx->env);
+}
+
+static int snp_compress(struct crypto_tfm *tfm, const u8 *src,
+			    unsigned int slen, u8 *dst, unsigned int *dlen)
+{
+	struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
+	size_t olen;
+	int err;
+
+	/* XXXX very pessimistic. check in snappy? */
+	if (*dlen < snappy_max_compressed_length(*dlen))
+		return -EINVAL;
+	err = snappy_compress(&ctx->env, src, slen, dst, &olen);
+	*dlen = olen;
+	return err;
+}
+
+static int snp_decompress(struct crypto_tfm *tfm, const u8 *src,
+			      unsigned int slen, u8 *dst, unsigned int *dlen)
+{
+	size_t ulen;
+
+	if (!snappy_uncompressed_length(src, slen, &ulen))
+		return -EIO;
+	if (*dlen < ulen)
+		return -EINVAL;
+	*dlen = ulen;
+	return snappy_uncompress(src, slen, dst) ? 0 : -EIO;
+}
+
+static struct crypto_alg alg = {
+	.cra_name		= "snappy",
+	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
+	.cra_ctxsize		= sizeof(struct snappy_ctx),
+	.cra_module		= THIS_MODULE,
+	.cra_list		= LIST_HEAD_INIT(alg.cra_list),
+	.cra_init		= snappy_init,
+	.cra_exit		= snappy_exit,
+	.cra_u			= { .compress = {
+	.coa_compress 		= snp_compress,
+	.coa_decompress  	= snp_decompress } }
+};
+
+static int __init snappy_mod_init(void)
+{
+	return crypto_register_alg(&alg);
+}
+
+static void __exit snappy_mod_fini(void)
+{
+	crypto_unregister_alg(&alg);
+}
+
+module_init(snappy_mod_init);
+module_exit(snappy_mod_fini);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Snappy Compression Algorithm");
-- 
1.7.7.4

  parent reply	other threads:[~2012-01-13  0:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-13  0:28 Updated btrfs/crypto snappy interface ready for merging Andi Kleen
2012-01-13  0:28 ` [PATCH 1/3] Add the snappy-c compressor to lib v2 Andi Kleen
2012-02-14  4:29   ` Mitch Harder
2012-02-14 19:52     ` Andi Kleen
2012-02-15  0:43       ` Mitch Harder
2012-01-13  0:28 ` [PATCH 2/3] BTRFS: Add snappy support v2 Andi Kleen
2012-01-13  0:28 ` Andi Kleen [this message]
2012-01-13  2:40 ` Updated btrfs/crypto snappy interface ready for merging Jaromir Zdrazil
2012-01-16 13:54 ` Chris Mason
2012-01-17  8:44   ` Li Zefan
2012-01-17  8:46     ` Andi Kleen
2012-01-17  8:56       ` Li Zefan
2012-01-17  9:27       ` Li Zefan
2012-01-17  9:51         ` Andi Kleen
2012-01-17  9:07 ` David Sterba
2012-01-18 15:02   ` evergreen
2012-01-24 17:03     ` Hugo Chevrain
2012-01-25 11:30       ` David Sterba
2012-01-18 15:05 ` Markus F.X.J. Oberhumer
2012-01-23 16:19   ` ANN: linux-kernel-lzo-2.06.20120123 - update LZO to v2.06 Markus F.X.J. Oberhumer
2012-01-23 19:12     ` Nitin Gupta
2012-01-25  1:36     ` Andi Kleen
2012-10-09  8:39       ` David Sterba
2012-07-16 18:30   ` ANNOUNCE: linux-kernel-lzo-20120716 - update LZO Markus F.X.J. Oberhumer
2012-07-19 20:58     ` richard -rw- weinberger
  -- strict thread matches above, loose matches on Subject: below --
2011-10-20 23:38 [PATCH 1/3] Add the snappy-c compressor to lib Andi Kleen
2011-10-20 23:39 ` [PATCH 3/3] Add snappy interface to crypto API Andi Kleen
2011-10-21 13:24   ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1326414530-10789-4-git-send-email-andi@firstfloor.org \
    --to=andi@firstfloor.org \
    --cc=ak@linux.intel.com \
    --cc=chris.mason@oracle.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).